UITableView 分隔符通过单元格重用错误绘制? [英] UITableView separators drawn incorrectly through cell reuse?

查看:30
本文介绍了UITableView 分隔符通过单元格重用错误绘制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个带有故事板的 iOS 5 项目,因此在 IB 中使用动态表格单元格原型.在其中一个视图中,我有一个带有可变高度单元格的表格视图,单元格高度根据内容的高度计算.

I'm working on an iOS 5 project with storyboards, and thus using dynamic table cell prototypes in IB. In one of the views, I've got a table view with variable-height cells, with the cell height calculated from the height of the contents.

tableView:heightForRowAtIndexPath: 在第一次显示表格视图时返回所有单元格的正确值.向下滚动几个项目时一切都很好,但后来出了点问题:实际的单元格似乎是正确的高度,包括它们的触摸区域,但它们的分隔符呈现在错误的位置(在单元格内部而不是它们之间).

tableView:heightForRowAtIndexPath: returns correct values for all cells when the table view is first displayed. All is well when scrolling down for a few items, but then something goes amiss: the actual cells seem to be correct height, including their touch areas, but their separators are rendered in the wrong places (inside cells instead of between them).

从测量分隔符的位置来看,似乎细胞重用可能与此有关.前三个单元格的分隔符已正确呈现,但第四个单元格的分隔符未正确呈现.heightForRowAtIndexPath: 为其返回正确的高度(在本例中为 125 像素),并且其包含的子视图都在正确的位置.但是,分隔符仅呈现与前一个分隔符的 108 像素,将其放置在单元格的 125 像素高区域内.

From measuring the separator placement, it appears as if cell reuse might have something to do with this. The separators for the first three cells are rendered correctly, but not the fourth one. heightForRowAtIndexPath: returns the correct height for it (125 pixels in this case), and its contained subviews are all in their right places. However, the separator is rendered only 108 pixels from the previous separator, placing it inside the 125 pixel high area of the cell.

这里的关键是:108px 是第一个表格单元格的高度,现在看不见了,可能会重复使用.我没有明确的证据,但似乎表格视图忽略了这些单元格的 heightForRowAtIndexPath: 并且只是根据重用的单元格高度呈现分隔符.

Here's the kicker: 108px is the height of the first table cell, now out of sight and probably reused. I don't have definite proof of this, but it appears that the table view is ignoring heightForRowAtIndexPath: for these cells and is just rendering the separator according to the reused cell height.

这并不能解释为什么一堆后来的、较短的单元格根本不呈现分隔符.但这就是我所要做的.

This doesn't explain why a bunch of later, shorter cells are not rendered separators at all. But this is all I've got to go on.

是否有解决方法、IB 设置或其他可能有帮助的方法?

Is there a workaround, an IB setting or something else that might help?

推荐答案

我遇到了同样的问题,分隔符显示在看似随机的位置.

I had the same problem where the separators were showing at seemingly random positions.

结果是我覆盖了layoutSubviews,但忘记调用[super layoutSubviews].添加该调用解决了我的问题.

It turned out the problem was that I was overriding layoutSubviews but forgot to call [super layoutSubviews]. Adding that call fixed the issue for me.

这篇关于UITableView 分隔符通过单元格重用错误绘制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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