简体   繁体   English

为什么UITableViewCell会在改变高度时闪烁?

[英]Why does UITableViewCell flash on changing height?

I have a simple UITableView with UITableViewCells. 我有一个简单的UITableView与UITableViewCells。 For the 0th row in the tableview, I have set a background color for the cell. 对于tableview中的第0行,我为单元格设置了背景颜色。 Now on tapping the 0th row I just want the row to expand. 现在点击第0行我只想要扩展行。 In order to achieve this, I'm using the same technique as described here to change the 0th row height. 为了实现这一点,我使用与此处描述的相同的技术来改变第0行的高度。

Now the problem is that the cell expands and collapses, but with each process there's an ugly flash/flicker on the cell. 现在的问题是细胞膨胀和坍塌,但每个过程都会在细胞上出现难看的闪烁/闪烁。 I've tried both beginupdates/endupdates and also reloading just that single row using [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone]; 我尝试了beginupdates / endupdates,并使用[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];重新加载该行[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];

What's causing the flicker/flash on tapping? 是什么导致点击闪烁/闪烁? And how do I get rid of it? 我怎么摆脱它呢? And yes, I forgot to mention, I have set the cell's selection style to none. 是的,我忘了提及,我已将单元格的选择样式设置为无。 So that's not causing this. 所以这不会造成这种情况。

I had the same issue when using non-opaque background color of the cell. 当使用单元格的非不透明背景颜色时,我遇到了同样的问题。 The solution is to use backgroundView property, instead of backgroundColor : 解决方案是使用backgroundView属性,而不是backgroundColor

UIView *backgroundView = [[UIView alloc] initWithFrame:cell.frame];
backgroundView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.1f];
cell.backgroundView = backgroundView;
cell.backgroundColor = [UIColor clearColor];

I've had a similar issue, with a few culprits: 我有一个类似的问题,有几个罪魁祸首:

  • Make sure all your tableview calls are on the main thread. 确保所有的tableview调用都在主线程上。
  • Check your cell height logic, if there are inconsistencies it could cause similar issues. 检查您的单元格高度逻辑,如果存在不一致,则可能导致类似问题。 Remember the cell heights need to be updated to the final heights before you ask the table to reload rows. 请记住,在要求表重新加载行之前,需要将单元格高度更新到最终高度。
  • Use a solid cell background color. 使用实心单元格背景颜色。 iOS 7 seems to have issues if the cell background color is not fully opaque. 如果单元格背景颜色不完全不透明,iOS 7似乎有问题。 (I couldn't find a solution to this while while using a non-opaque bg color. I suspect it may be an iOS 7 bug.) (当我使用非透明的bg颜色时,我无法找到解决方案。我怀疑它可能是iOS 7的错误。)

If you don't want animation and stop the cell from flashing, you can use following solution - 如果您不想要动画并停止单元格闪烁,可以使用以下解决方案 -

     [UIView setAnimationsEnabled:NO];
     [self.tableView beginUpdates];

     [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil]
                                               withRowAnimation:UITableViewRowAnimationNone];
     [self.tableView endUpdates];
     [UIView setAnimationsEnabled:YES];

The above snippet will have no animation on cells while reloading the cell 重新加载单元格时,上面的代码片段在单元格上没有动画

I think you should not use beginUpdate/endUpdates methods and perform animations manually before calling reloadData. 我认为你不应该使用beginUpdate / endUpdates方法并在调用reloadData之前手动执行动画。 The final state of cells (after animation) are defined with datasource. 单元格的最终状态(动画之后)是使用数据源定义的。 I hope this will helps you. 我希望这会对你有所帮助。

I'm not sure if this is the same problem as I faced today morning but my solution may help somebody... 我不确定这是否与我今天早上遇到的问题相同,但我的解决方案可能对某人有所帮助......

My subviews disappeared during reloading with animation... It didn't mattter if I used 我的子视图在重新加载动画期间消失了...如果我使用的话它没有消息

[self.tableView beginUpdates];
[self.tableView endUpdates];

or 要么

[self.tableView reloadRowsAtIndexPaths:]

Finally I found out that I modified frames of the subviews of the cell for calculating the proper sizes... I changed it and started using other views for calculations and it solved my problem. 最后我发现我修改了单元格子视图的帧以计算合适的大小...我改变它并开始使用其他视图进行计算,它解决了我的问题。

Instead of: 代替:

self.leftLabel.frame = CGRectMake(0, 0, innerWidth, CGFLOAT_MAX);
[self.leftLabel sizeToFit];

I made calculations this way: 我这样做了计算:

UILabel * tmpLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, innerWidth, CGFLOAT_MAX)];
tmpLabel.numberOfLines = self.leftLabel.numberOfLines;
tmpLabel.attributedText = self.leftLabel.attributedText;
[tmpLabel sizeToFit];

self.leftLabel.frame = tmpLabel.frame;

I would expect the flash because you've chosen no animation, ie, UITableViewRowAnimationNone . 我期待flash,因为你没有选择动画,即UITableViewRowAnimationNone Try the fade animation. 尝试淡入淡出动画。

[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade]];

If that still doesn't look good, try animating all the visible rows: 如果仍然看起来不太好,请尝试设置所有可见行的动画:

[self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationFade]];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM