我正在使用自定义UITableViewCell,它有一些标签,按钮和图像视图可供显示.单元格中有一个标签,其文本是NSString
对象,字符串的长度可以是可变的.由于这一点,我无法设置恒定的高度到细胞中UITableView
的heightForCellAtIndex
方法.细胞的高度取决于这可以使用被确定标签的高度NSString
的sizeWithFont
方法.我尝试过使用它,但看起来我在某个地方出错了.怎么修好?
这是用于初始化单元格的代码.
if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) { self.selectionStyle = UITableViewCellSelectionStyleNone; UIImage *image = [UIImage imageNamed:@"dot.png"]; imageView = [[UIImageView alloc] initWithImage:image]; imageView.frame = CGRectMake(45.0,10.0,10,10); headingTxt = [[UILabel alloc] initWithFrame: CGRectMake(60.0,0.0,150.0,post_hdg_ht)]; [headingTxt setContentMode: UIViewContentModeCenter]; headingTxt.text = postData.user_f_name; headingTxt.font = [UIFont boldSystemFontOfSize:13]; headingTxt.textAlignment = UITextAlignmentLeft; headingTxt.textColor = [UIColor blackColor]; dateTxt = [[UILabel alloc] initWithFrame:CGRectMake(55.0,23.0,150.0,post_date_ht)]; dateTxt.text = postData.created_dtm; dateTxt.font = [UIFont italicSystemFontOfSize:11]; dateTxt.textAlignment = UITextAlignmentLeft; dateTxt.textColor = [UIColor grayColor]; NSString * text1 = postData.post_body; NSLog(@"text length = %d",[text1 length]); CGRect bounds = [UIScreen mainScreen].bounds; CGFloat tableViewWidth; CGFloat width = 0; tableViewWidth = bounds.size.width/2; width = tableViewWidth - 40; //fudge factor //CGSize textSize = {width, 20000.0f}; //width and height of text area CGSize textSize = {245.0, 20000.0f}; //width and height of text area CGSize size1 = [text1 sizeWithFont:[UIFont systemFontOfSize:11.0f] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap]; CGFloat ht = MAX(size1.height, 28); textView = [[UILabel alloc] initWithFrame:CGRectMake(55.0,42.0,245.0,ht)]; textView.text = postData.post_body; textView.font = [UIFont systemFontOfSize:11]; textView.textAlignment = UITextAlignmentLeft; textView.textColor = [UIColor blackColor]; textView.lineBreakMode = UILineBreakModeWordWrap; textView.numberOfLines = 3; textView.autoresizesSubviews = YES; [self.contentView addSubview:imageView]; [self.contentView addSubview:textView]; [self.contentView addSubview:webView]; [self.contentView addSubview:dateTxt]; [self.contentView addSubview:headingTxt]; [self.contentView sizeToFit]; [imageView release]; [textView release]; [webView release]; [dateTxt release]; [headingTxt release]; }
这是高度和宽度出错的标签:
textView = [[UILabel alloc] initWithFrame:CGRectMake(55.0,42.0,245.0,ht)];
rpetrich.. 498
你UITableViewDelegate
应该实施tableView:heightForRowAtIndexPath:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return [indexPath row] * 20; }
您可能希望使用NSString
的sizeWithFont:constrainedToSize:lineBreakMode:
方法来计算行高,而不是仅仅在indexPath上执行一些愚蠢的数学运算:)
你UITableViewDelegate
应该实施tableView:heightForRowAtIndexPath:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return [indexPath row] * 20; }
您可能希望使用NSString
的sizeWithFont:constrainedToSize:lineBreakMode:
方法来计算行高,而不是仅仅在indexPath上执行一些愚蠢的数学运算:)
如果所有行都是相同的高度,只需设置rowHeight
UITableView 的属性而不是实现heightForRowAtIndexPath
.Apple Docs:
使用tableView:heightForRowAtIndexPath:而不是rowHeight会对性能产生影响.每次显示表视图时,它会在其每个行的委托上调用tableView:heightForRowAtIndexPath:这会导致具有大量行(大约1000或更多)的表视图出现严重的性能问题.
在自定义UITableViewCell控制器中添加此项
-(void)layoutSubviews { CGRect newCellSubViewsFrame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height); CGRect newCellViewFrame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height); self.contentView.frame = self.contentView.bounds = self.backgroundView.frame = self.accessoryView.frame = newCellSubViewsFrame; self.frame = newCellViewFrame; [super layoutSubviews]; }
在UITableView -controller中添加此项
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return [indexPath row] * 1.5; // your dynamic height... }
#define FONT_SIZE 14.0f #define CELL_CONTENT_WIDTH 300.0f #define CELL_CONTENT_MARGIN 10.0f - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; { /// Here you can set also height according to your section and row if(indexPath.section==0 && indexPath.row==0) { text=@"pass here your dynamic data"; CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f); CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; CGFloat height = MAX(size.height, 44.0f); return height + (CELL_CONTENT_MARGIN * 2); } else { return 44; } } - (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell; UILabel *label = nil; cell = [tv dequeueReusableCellWithIdentifier:@"Cell"]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell"]; } ********Here you can set also height according to your section and row********* if(indexPath.section==0 && indexPath.row==0) { label = [[UILabel alloc] initWithFrame:CGRectZero]; [label setLineBreakMode:UILineBreakModeWordWrap]; [label setMinimumFontSize:FONT_SIZE]; [label setNumberOfLines:0]; label.backgroundColor=[UIColor clearColor]; [label setFont:[UIFont systemFontOfSize:FONT_SIZE]]; [label setTag:1]; // NSString *text1 =[NSString stringWithFormat:@"%@",text]; CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f); CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; if (!label) label = (UILabel*)[cell viewWithTag:1]; label.text=[NSString stringWithFormat:@"%@",text]; [label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))]; [cell.contentView addSubview:label]; } return cell; }
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; { CGSize constraintSize = {245.0, 20000} CGSize neededSize = [ yourText sizeWithFont:[UIfont systemFontOfSize:14.0f] constrainedToSize:constraintSize lineBreakMode:UILineBreakModeCharacterWrap] if ( neededSize.height <= 18) return 45 else return neededSize.height + 45 //18 is the size of your text with the requested font (systemFontOfSize 14). if you change fonts you have a different number to use // 45 is what is required to have a nice cell as the neededSize.height is the "text"'s height only //not the cell. }
我看到了很多解决方案,但都是错误的或不完整的.您可以在viewDidLoad和autolayout中解决5行的所有问题.这对于客观C:
_tableView.delegate = self; _tableView.dataSource = self; self.tableView.estimatedRowHeight = 80;//the estimatedRowHeight but if is more this autoincremented with autolayout self.tableView.rowHeight = UITableViewAutomaticDimension; [self.tableView setNeedsLayout]; [self.tableView layoutIfNeeded]; self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0) ;
对于swift 2.0:
self.tableView.estimatedRowHeight = 80 self.tableView.rowHeight = UITableViewAutomaticDimension self.tableView.setNeedsLayout() self.tableView.layoutIfNeeded() self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0)
现在使用xib创建您的单元格或在Storyboard中创建tableview使用此功能,您无需再执行任何操作或覆盖.(不要忘记数字os行0)和底部标签(约束)降级"内容拥抱优先级 - 垂直到250"
您可以在下一个网址中下载代码:https: //github.com/jposes22/exampleTableCellCustomHeight
参考文献:http://candycode.io/automatically-resizing-uitableviewcells-with-dynamic-text-height-using-auto-layout/
要为行高和估计的行高设置自动尺寸,请确保执行以下步骤,自动尺寸对单元格/行高布局有效。
分配和执行的tableview数据源和委托
分配UITableViewAutomaticDimension
为rowHeight和estimatedRowHeight
实现委托/数据源方法(即向其heightForRowAt
返回值UITableViewAutomaticDimension
)
--
目标C:
// in ViewController.h #import@interface ViewController : UIViewController @property IBOutlet UITableView * table; @end // in ViewController.m - (void)viewDidLoad { [super viewDidLoad]; self.table.dataSource = self; self.table.delegate = self; self.table.rowHeight = UITableViewAutomaticDimension; self.table.estimatedRowHeight = UITableViewAutomaticDimension; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewAutomaticDimension; }
迅速:
@IBOutlet weak var table: UITableView! override func viewDidLoad() { super.viewDidLoad() // Don't forget to set dataSource and delegate for table table.dataSource = self table.delegate = self // Set automatic dimensions for row height table.rowHeight = UITableViewAutomaticDimension table.estimatedRowHeight = UITableViewAutomaticDimension } // UITableViewAutomaticDimension calculates height of label contents/text func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return UITableViewAutomaticDimension }
对于UITableviewCell中的标签实例
设置行数= 0(&换行模式=截尾)
设置关于其superview /单元容器的所有约束(上,下,左左)。
可选:即使没有数据,如果您希望标签覆盖最小的垂直区域,请设置标签的最小高度。
注意:如果您具有多个具有动态长度的标签(UIElements),则应根据其内容大小进行调整:对于要以更高优先级进行扩展/压缩的标签,请调整“内容拥抱和抗压缩优先级”。
在此示例中,我设置了较低的拥抱和较高的抗压缩优先级,这导致为第二个(黄色)标签的内容设置了更高的优先级/重要性。
感谢关于此主题的所有帖子,有一些非常有用的方法来调整UITableViewCell的rowHeight.
以下是其他人的一些概念汇编,这些概念在构建iPhone和iPad时非常有用.您还可以访问不同的部分,并根据不同的视图大小进行调整.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { int cellHeight = 0; if ([indexPath section] == 0) { cellHeight = 16; settingsTable.rowHeight = cellHeight; } else if ([indexPath section] == 1) { cellHeight = 20; settingsTable.rowHeight = cellHeight; } return cellHeight; } else { int cellHeight = 0; if ([indexPath section] == 0) { cellHeight = 24; settingsTable.rowHeight = cellHeight; } else if ([indexPath section] == 1) { cellHeight = 40; settingsTable.rowHeight = cellHeight; } return cellHeight; } return 0; }