ios - UITableViewCell subview 在使用大小类时报告不正确的宽度

标签 ios uitableview swift xcode6

出于某种原因,在 xcode 6 中使用尺寸等级时,我的单元格中的 subview 宽度不正确。我有一个 UIImageView,带有用于调整大小的自动布局(常量:顶部、左/右、底部为 10)。

当从 tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) 调用以下内容时:

println("w: \(cell.mapSnapshotImageView.bounds.size.width) h: \(cell.mapSnapshotImageView.bounds.size.height)")

我总是得到:

w: 580.0 h: 80.0

尽管它应该w: 300.0 h: 80.0,基于我在 iPhone 5S 上的 320x100 电池。

一切都显示正确,但我想使用 subview 的宽度来计算其他 subview 。关于为什么会发生这种情况的任何想法?错误报告或按设计工作?

编辑:

此问题仅适用于最初加载的单元格。当细胞被重复使用时,问题就不会出现了。 println 重用单元格的输出:

w: 300.0 h: 80.0

最佳答案

在访问帧之前调用 willDisplayCell:forRowAtIndexPath 中的 cell.layoutIfNeeded()。这样就可以了。

关于ios - UITableViewCell subview 在使用大小类时报告不正确的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25559732/

相关文章:

iOS慢速图像像素迭代

cocoa-touch - willAnimateRotationToInterfaceOrientation 在 vi​​ewWillAppear 之前被调用

ios - 如何允许编辑由 RxDataSources 支持的 TableView ?

swift - 函数在完成 block 完成之前返回

swift - 如何在全局范围内保值?

ios - NSDate - 将日期转换为 GMT

ios - CocoaPods Swift 链接器错误

ios - ScrollView 错误行为之上的 TableView

ios - UITableView 和 UICollectionView 在一行中重用第一行中的集合

ios - dataTaskWithRequest 因 302 重定向响应而崩溃 - Swift