日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[ios]UITableViewCell自适应高度 【转】

發布時間:2023/12/18 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [ios]UITableViewCell自适应高度 【转】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
??? // 列寬
??? CGFloat contentWidth = self.tableView.frame.size.width;
??? // 用何種字體進行顯示
??? UIFont *font = [UIFont systemFontOfSize:13];
????
??? // 該行要顯示的內容
??? NSString *content = [data objectAtIndex:indexPath.row];
??? // 計算出顯示完內容需要的最小尺寸
??? CGSize size = [content sizeWithFont:font constrainedToSize:CGSizeMake(contentWidth, 1000) lineBreakMode:UILineBreakModeWordWrap];
????
??? // 這裏返回需要的高度
??? return size.height;?
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
????
??? static NSString *CellIdentifier = @"Cell";
??? // 列寬
??? CGFloat contentWidth = self.tableView.frame.size.width;
??? // 用何種字體進行顯示
??? UIFont *font = [UIFont systemFontOfSize:13];
????
??? // 該行要顯示的內容
??? NSString *content = [data objectAtIndex:indexPath.row];
??? // 計算出顯示完內容需要的最小尺寸
??? CGSize size = [content sizeWithFont:font constrainedToSize:CGSizeMake(contentWidth, 1000) lineBreakMode:UILineBreakModeWordWrap];
????
??? // 構建顯示行
??? UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
??? if (cell == nil) {
??????? cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
??? }
????
??? CGRect rect = [cell.textLabel textRectForBounds:cell.textLabel.frame limitedToNumberOfLines:0];
??? // 設置顯示榘形大小
??? rect.size = size;
??? // 重置列文本區域
??? cell.textLabel.frame = rect;
????
??? cell.textLabel.text = content;
????
??? // 設置自動換行(重要)
??? cell.textLabel.numberOfLines = 0;
??? // 設置顯示字體(一定要和之前計算時使用字體一至)
??? cell.textLabel.font = font;

??? return cell;
}

?

//===========//

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];//這個frame是初設的,沒關系,后面還會重新設置其size
[label setNumberOfLines:0];
NSString *s = @"abcdefghijklmn";
UIFont *font = [UIFont fontWithName:@"Arial" size:12];
CGSize size = CGSizeMake(320,2000);
CGSize labelsize = [s sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];
[label setFrame:CGRectMake(0,0, labelsize.width, labelsize.height)];
[self.view addSubView:label];

?

=============gengxing==========///

主要有兩個地方需要調整高度,一個是自己創建的UILabel或其它,另一個就是cell的高度。在創建cell的地方只需要定義好label的屬性就行了:

  • -?(UITableViewCell?*)tableView:(UITableView?*)tableView?cellForRowAtIndexPath:(NSIndexPath?*)indexPath??
  • {??
  • ????static?NSString?*cellIdentifier?=?@"cell";??
  • ????UITableViewCell?*cell?=?[tableView?dequeueReusableCellWithIdentifier:cellIdentifier];??
  • ????if?(cell?==?nil)??
  • ????{??
  • ????????cell?=?[[[UITableViewCell?alloc]?initWithFrame:CGRectZero?reuseIdentifier:cellIdentifier]?autorelease];??
  • ??????????
  • ????????UILabel?*contentLabel?=?[[UILabel?alloc]?initWithFrame:CGRectZero];??
  • ????????[contentLabel?setLineBreakMode:UILineBreakModeWordWrap];??
  • ????????[contentLabel?setNumberOfLines:0];??
  • ????????[contentLabel?setFont:FONT_CELL];??
  • ????????[contentLabel?setTag:TAG_LABEL];??
  • ??????????
  • ????????[[cell?contentView]?addSubview:contentLabel];??
  • ????????[contentLabel?release];??
  • ????}??
  • 設置完值后再設置frame:

  • cell.textLabel.text?=?@"";??
  • CGSize?captionSize?=?[cell.textLabel.text?sizeWithFont:FONT_CELL];??
  • contentLabel.frame?=?CGRectMake(captionSize.width?+?10,?0,???
  • ????????????????????????????????CGRectGetWidth(cell.bounds)?-?captionSize.width?-?10,???
  • ????????????????????????????????[self?tableView:nil?heightForRowAtIndexPath:[NSIndexPath?indexPathForRow:indexPath.row?inSection:0]]);??
  • 我需要讓cell的高度去適應label,所以調用計算cell高度的方法,在heightForRowAtIndexPath方法中計算label所需的高度即可:

    ?

  • -(CGFloat)tableView:(UITableView?*)tableView?heightForRowAtIndexPath:(NSIndexPath?*)indexPath{??
  • ????if?(indexPath.row?<?…)?{??
  • ????????return?90;??
  • ????}?else?if?(indexPath.row?==?...)?{??
  • ????????NSString?*caption?=?@"";??
  • ????????CGSize?contentSize?=?getTextSize(caption,?FONT_CELL,?@"data",??
  • ?????????????????????????????????????????CGRectGetWidth(self.tableView.bounds));??
  • ????????return?contentSize.height?>?44???contentSize.height?:?44;??
  • ????}??
  • ????return?44;??
  • }??
  • 為了不讓高度過于混亂,我把最小值設為44,getTextSize只是一個工具方法,用于計算x軸偏移的距離:

    ?

  • CGSize?getTextSize(NSString?*offsetText,UIFont?*font,NSString?*text,?CGFloat?maxWidth){??
  • ????CGSize?offsetTextSize?=?[offsetText?sizeWithFont:font];??
  • ????CGSize?textSize?=?[text?sizeWithFont:font???
  • ?????constrainedToSize:CGSizeMake(maxWidth-offsetTextSize.width,?MAXFLOAT)???
  • ?????????lineBreakMode:UILineBreakModeWordWrap];??
  • ????return?textSize;??
  • }??
  • 期間使用了一些公共宏,比如:FONT_CELL等等,只在cellForRowAtIndexPath里面設置好label的基本屬性和frame,計算高度就交給heightForRowAtIndexPath,可以自己對返回值進行修改。

    轉載于:https://www.cnblogs.com/jinjiantong/archive/2013/03/22/2975077.html

    總結

    以上是生活随笔為你收集整理的[ios]UITableViewCell自适应高度 【转】的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。