【发布时间】:2016-06-30 07:02:15
【问题描述】:
在我的应用程序中,我有一个UITableViewController,其单元格根据其内容(整体图像和文本)非常动态,我需要计算heightForRowAtIndexPath() 中单元格的高度。为了得到这个,我需要按标识符出列一个单元格。我不能简单地实例化其自定义类的单元格,因为它缺少接口生成器中定义的约束,并且演算不正确。相反,我知道在heightForRowAtIndexPath() 中调用dequeueReusableCellWithIdentifier() 会导致无限递归循环。因此,阅读了这里的一些帖子和 Ray Wenderlich 的一些教程,我能够在 heightForRowAtIndexPath() 中编写这段代码
var token: dispatch_once_t = 0
var cell = nil
dispatch_once(&token) {
cell = tableView.dequeueReusableCellWithIdentifier("BigTextCell", forIndexPath: indexPath) as! BigTextCell
}
/*adjust cell constraints and return its height*/
这似乎对每个人都有效(至少在 Objective-C 中),但我不知道为什么它不适合我。正如预期的那样,它在无限循环中阻塞。为什么? 我必须承认我从未使用过单例,而且我对他们不熟悉,但我认为这是正确的。 有什么想法吗?
更新
我刚刚尝试了以下方法,但它不起作用:
var cell:BigTextCell!
dispatch_once(&onceToken) {
cell = tableView.dequeueReusableCellWithIdentifier("BigTextCell", forIndexPath: indexPath) as! BigTextCell
}
for constraint in cell.paragraph.constraints {
if (constraint.identifier == "longTextConstraint") {
constraint.constant = cell.paragraph.sizeThatFits(CGSize(width: cell.paragraph.frame.width, height: CGFloat.max)).height
return constraint.constant
}
}
【问题讨论】:
标签: ios objective-c swift uitableview singleton