设置自定义 UITableViewCells 高度
我正在使用自定义 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)];
I am using a custom UITableViewCell which has some labels, buttons and image views to be displayed. There is one label in the cell whose text is a NSString
object and the length of string could be variable. Due to this, I cannot set a constant height to the cell in the UITableView
's heightForCellAtIndex
method. The cell's height depends on the label's height which can be determined using the NSString
's sizeWithFont
method. I tried using it, but it looks like I'm going wrong somewhere. How can it be fixed?
Here is the code used for initializing the cell.
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];
}
This is the label whose height and width are going wrong:
textView = [[UILabel alloc] initWithFrame:CGRectMake(55.0,42.0,245.0,ht)];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您的
UITableViewDelegate
应该实现tableView:heightForRowAtIndexPath:
Objective-C
Swift 5
您可能想要使用
NSString
的sizeWithFont:constrainedToSize:lineBreakMode:
方法来计算行高,而不是仅仅在 indexPath 上执行一些愚蠢的数学:)Your
UITableViewDelegate
should implementtableView:heightForRowAtIndexPath:
Objective-C
Swift 5
You will probably want to use
NSString
'ssizeWithFont:constrainedToSize:lineBreakMode:
method to calculate your row height rather than just performing some silly math on the indexPath :)如果所有行的高度相同,只需设置 UITableView 的
rowHeight
属性,而不是实现heightForRowAtIndexPath
。 苹果文档:If all your rows are the same height, just set the
rowHeight
property of the UITableView rather than implementing theheightForRowAtIndexPath
. Apple Docs:在自定义 UITableViewCell -controller 中添加此
在 UITableView -controller 中添加此
in a custom UITableViewCell -controller add this
In the UITableView -controller add this
我看到了很多解决方案,但都是错误的或不完整的。
viewDidLoad和autolayout中的5行就可以解决所有问题。
这适用于目标 C:
适用于 swift 2.0:
现在使用 xib 创建单元格或在 Storyboard 中的 tableview 中创建单元格
有了这个,您无需再执行任何操作或覆盖。 (不要忘记数字操作系统行 0)和底部标签(约束)降级“内容拥抱优先级 - 垂直到 250”
您可以在下一个网址下载代码:
https://github.com/jposes22/exampleTableCellCustomHeight
I saw a lot of solutions but all was wrong or uncomplet.
You can solve all problems with 5 lines in viewDidLoad and autolayout.
This for objetive C:
For swift 2.0:
Now create your cell with xib or into tableview in your Storyboard
With this you no need implement nothing more or override. (Don forget number os lines 0) and the bottom label (constrain) downgrade "Content Hugging Priority -- Vertical to 250"
You can donwload the code in the next url:
https://github.com/jposes22/exampleTableCellCustomHeight
设置行高和自动尺寸 估计行高,确保执行以下步骤,使自动尺寸对单元格/行高布局有效。
UITableViewAutomaticDimension
分配给 rowHeight 和 delegate estimatedRowHeightheightForRowAt
并返回一个值UITableViewAutomaticDimension
)-
Objective C:
Swift:
对于 UITableviewCell 中的标签实例,
注意:如果您有多个带有动态标签 (UIElements)长度,应根据内容大小进行调整:为要以更高优先级扩展/压缩的标签调整“内容拥抱和压缩阻力优先级”。
在此示例中,我设置了低拥抱和高压缩阻力优先级,这导致为第二个(黄色)标签的内容设置更多优先级/重要性。
To set automatic dimension for row height & estimated row height, ensure following steps to make, auto dimension effective for cell/row height layout.
UITableViewAutomaticDimension
to rowHeight & estimatedRowHeightheightForRowAt
and return a valueUITableViewAutomaticDimension
to it)-
Objective C:
Swift:
For label instance in UITableviewCell
Note: If you've more than one labels (UIElements) with dynamic length, which should be adjusted according to its content size: Adjust 'Content Hugging and Compression Resistance Priority` for labels which you want to expand/compress with higher priority.
Here in this example I set low hugging and high compression resistance priority, that leads to set more priority/importance for contents of second (yellow) label.
感谢有关此主题的所有帖子,有一些非常有用的方法来调整 UITableViewCell 的 rowHeight。
以下是其他人的一些概念的汇编,这些概念在针对 iPhone 和 iPad 进行构建时确实很有帮助。 您还可以访问不同的部分并根据不同的视图大小进行调整。
Thanks to all the posts on this topic, there are some really helpful ways to adjust the rowHeight of a UITableViewCell.
Here is a compilation of some of the concepts from everyone else that really helps when building for the iPhone and iPad. You can also access different sections and adjust them according to the varying sizes of views.
要随着标签文本的增加而动态单元格高度,您首先需要计算文本将在
-heightForRowAtIndexPath
委托方法中使用的高度,并将其与其他标签、图像的添加高度一起返回(文本的最大高度+其他静态组件的高度)并在单元格创建中使用相同的高度。To have the dynamic cell height as the text of Label increases, you first need to calculate height,that the text gonna use in
-heightForRowAtIndexPath
delegate method and return it with the added heights of other lables,images (max height of text+height of other static componenets) and use same height in cell creation.