如何在heightForRowAtIndexPath中获取UITableViewCell? [英] how to obtain the UITableViewCell within heightForRowAtIndexPath?

查看:531
本文介绍了如何在heightForRowAtIndexPath中获取UITableViewCell?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 heightForRowAtIndexPath 方法中获得 UITableViewCell ,即给定indexPath?

How does one obtain the UITableViewCell when within the heightForRowAtIndexPath method, i.e. given the indexPath?

(然后我可以访问我创建的内容视图以增加其高度)

(then I could access the content views I have created to add their heights up)

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  // How to get the UITableViewCell associated with this indexPath?
}

谢谢

编辑:事实上,确实有一个有效的方法来做到这一点?当我放入一些 NSLog 语句时,似乎 heightForRowAtIndexPath 在调用之前多次调用它cellForRowAtIndexPath (这是我在单元格中设置 UILabels 的地方)?这种意味着我可能会尝试使用一种不起作用的技术,即我希望在 heightForRowAtIndexPath 中访问每个单元格中已创建的标签以获得它们的高度,将它们添加到整个单元格行高度,但是如果它们尚未设置(在 cellForRowAtIndexPath 中),那么我想我的方法不能真正起作用?

In fact is there really a valid way to do this? When I put some NSLog statements it seems that heightForRowAtIndexPath it called several times before the calls to cellForRowAtIndexPath (which is where I set up the UILabels in the cell)? This kind implies that I may be tried to use a technique that will not work, i.e. I was hoping in heightForRowAtIndexPath to access the already created labels in each cell to get their heights and add them together for the overall cell row height, HOWEVER if they haven't been set up yet (within cellForRowAtIndexPath) then I guess my approach can't really work?

推荐答案

对于 iOS8

override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.estimatedRowHeight = 80
        self.tableView.rowHeight = UITableViewAutomaticDimension
    }

OR

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
            return UITableViewAutomaticDimension

    }

但是对于 iOS7 ,关键是自动布局后计算高度,

But for iOS7, the key is calculate the height after autolayout,

func calculateHeightForConfiguredSizingCell(cell: GSTableViewCell) -> CGFloat {
        cell.setNeedsLayout()
        cell.layoutIfNeeded()
        let height = cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingExpandedSize).height + 1.0
        return height

    }

注意:

1)如果有多行标签,请不要忘记将 numberOfLines 设置为0。

1) If multiple lines labels, don't forget set the numberOfLines to 0.

2)不要忘记 label.preferredMaxLayoutWidth = CGRectGetWidth(tableView.bounds)

这篇关于如何在heightForRowAtIndexPath中获取UITableViewCell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆