[英]How to calculate table height dynamically?
I'm trying to calculate height for a UITableView dynamically. 我正在尝试动态计算UITableView的高度。 I thought I could do it doing the sum on
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
, the problem is that this method is only called for the first X cells and won't be called again for a new set until you scroll. 我以为我可以在
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
上做总和,问题是此方法仅在前X个单元格中调用,而不会在新的X单元格中再次调用设置直到滚动。
I'm using autolayout constraints, How can I calculate table view accuratelly? 我正在使用自动布局约束,如何准确计算表格视图?
You can call below method to get height of UILabel.. 您可以调用以下方法来获取UILabel的高度。
- (CGFloat)heightForWidth:(float)width AndFont:(UIFont *)font ForString:(NSString *) yourTextValue
{
CGSize size = CGSizeZero;
if (yourTextValue.length > 0) {
CGRect frame = [yourTextValue boundingRectWithSize:CGSizeMake(width, 999999) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName: font } context:nil];
size = CGSizeMake(frame.size.width, frame.size.height + 1);
}
return size.height;
}
Another way to set dynamic height of a cell 设置单元格动态高度的另一种方法
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return UITableViewAutomaticDimension
}
I am not sure what you are trying to do but appending to your comment you can disable scroll view with: 我不确定您要做什么,但可以在注释后添加以下内容来禁用滚动视图:
self.tableView.scrollEnabled = true
And you can get the hight of all the cells with: 您可以通过以下方式获得所有单元格的最高价值:
self.tableView.contentSize.height
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.