iOS 8 UITableView第一行高度错误
我正在做一个应用程序,我面临一个奇怪的问题。 我在故事板中创build了一个UITableViewController并添加了一个原型单元格。 在这个单元格中,我添加了一个UILabel元素,这个UILabel占据了整个单元格。 我已经设置了自动布局,并添加了左,右,上,下约束。 UILabel包含一些文本。
现在在我的代码中,我初始化了表视图的rowHeight和estimatedRowHeight:
override func viewDidLoad() { super.viewDidLoad() self.tableView.rowHeight = UITableViewAutomaticDimension self.tableView.estimatedRowHeight = 50 }
我创build这个单元格如下:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell : UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("HelpCell") as? UITableViewCell if(cell == nil) { cell = UITableViewCell(style: .Default, reuseIdentifier: "HelpCell") } return cell! }
我在表格视图中返回两行。 这是我的问题:第一行的高度是很大的。 看来,第二,第三排等都有一个正确的高度。 我真的不明白为什么是这样。 有人可以帮我弄这个吗?
我有一个问题,第一次加载时单元的高度不正确,但上下滚动之后单元格的高度就被固定了。
我尝试了所有不同的“修复”这个问题,然后最终发现调用self.tableView.reloadData
后调用这些函数。
self.tableView.reloadData() // Bug in 8.0+ where need to call the following three methods in order to get the tableView to correctly size the tableViewCells on the initial load. self.tableView.setNeedsLayout() self.tableView.layoutIfNeeded() self.tableView.reloadData()
只有在初始加载后才能执行这些额外的布局调用。
我在这里find了非常有用的信息: https : //github.com/smileyborg/TableViewCellWithAutoLayoutiOS8/issues/10
更新:有时您可能还必须在heightForRowAtIndexPath
完全configuration您的单元格,然后返回计算的单元格高度。 看看这个链接的一个很好的例子, http: heightForRowAtIndexPath
,特别是heightForRowAtIndexPath
的部分。
更新2:我也发现它非常有利于覆盖estimatedHeightForRowAtIndexPath
并提供一些准确的行高估计。 这是非常有用的,如果你有一个单元格,可以是各种不同高度的UITableView
。
estimatedHeightForRowAtIndexPath
:这是一个人为的示例实现:
public override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { let cell = tableView.cellForRowAtIndexPath(indexPath) as! MyCell switch cell.type { case .Small: return kSmallHeight case .Medium: return kMediumHeight case .Large: return kLargeHeight default: break } return UITableViewAutomaticDimension }
更新3: UITableViewAutomaticDimension
已经修复了iOS 9(呜呜呜!)。 所以你的细胞应该自动resize而不必手动计算细胞的高度。
正如苹果在setNeedsLayout
的描述中所说:
此方法不会强制立即更新,而是等待下一个更新周期,您可以在更新任何视图之前使用它来使多个视图的布局无效。 此行为允许您将所有布局更新合并到一个更新周期,这通常对性能更好。
因此,您应该在dispatch_after block
添加所需的代码行(这应该在正确的布局中执行)(这会将您的方法放入RunLoop的队列中)。 而且你的代码将在需求布局适用后执行。
例:
- (void)someMethod { [self.tableView reloadData]; [self.tableView setNeedsLayout]; [self.tableView layoutIfNeeded]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ //code which should be executed with the right size of table });
在iOS 8中,为estimatedRowHeight
分配一个值会启用新的iOS 8自动行高计算function。 这意味着单元格的高度是由内部使用其内部约束得出的。 如果这些限制有问题,你会得到奇怪的结果。 所以你的单元约束有问题。 他们可能是模棱两可的; 这是通常的不一致的原因。 不过,我只能告诉你,因为你没有真正展示/描述约束条件。
我build议删除UILabel
的底部约束。 它将根据文本resize,并且单元格也应resize。
如果这没有解决问题,请尝试在viewDidLoad()
添加以下内容:
self.tableView.reloadData()
这对我有效
- (void)layoutSubviews { [super layoutSubviews]; // Your implementation }
- iOS中的Mutlitouch绘图
- 以编程方式closures折叠显示中的详细视图控制器?
- 在UIButton上的文本更改不会粘住
- 候选人有不匹配的typesUITableView?
- iBeacon蓝牙didEnterRegion和didExitRegion方法永远不会被触发
- UIRefreschControl的Glitchy动画,带有导航栏的大标题
- 如何检查一个数字是否是NSInteger或浮点在IOS?
- var someString =“Some String”,var someString:String =“Some String”,var someString =“Some String”as string
- UITableViewCell的UITextField子视图,获取单元格的indexPath