简体   繁体   English

UITableView具有随机消失的单元格

[英]UITableView has randomly disappearing cells

I have a fairly simple UITableView which uses an NSArray of flight bookings, and displays one cell per booking. 我有一个相当简单的UITableView,它使用航班预订的NSArray,并且每个预订显示一个单元格。 Recently customers have raised an issue (which I can only very occasionally reproduce) whereby some of the cells are completely hidden. 最近,客户提出了一个问题(我只能偶尔复制),其中一些单元格被完全隐藏了。 The space for them is there, but there's just a gap where they should be. 他们的空间就在那里,但应该有一个空隙。

The table has a header and footer so I can see the gaps (it's not just that there are less elements). 该表具有页眉和页脚,因此我可以看到间隙(不仅仅是元素更少)。

Example screenshot here: http://myflightsapp.com/images/missing-cells.png 此处的示例屏幕截图: http : //myflightsapp.com/images/missing-cells.png

It really is intermittent, because if I restart the app, the issue goes away. 这确实是断断续续的,因为如果我重新启动应用程序,问题就消失了。

I've tried replacing cellForRowAtIndexPath with a very simple 2 liner to just return a completely standard UITableViewCell with a text label, and it displays the same issue, so for me that rules out the Nib/Custom class being the issue. 我尝试用非常简单的2衬里替换cellForRowAtIndexPath,以返回带有文本标签的完全标准的UITableViewCell,并且它显示相同的问题,因此对我来说,排除Nib / Custom类是问题。

The heightForRowAtIndexPath always returns a constant, so it's not showing a zero height row by mistake. heightForRowAtIndexPath始终返回一个常量,因此不会误显示高度为零的行。

There's only ever 1 section to the table. 桌子上只有1个部分。

I've put debug in the numberOfSectionsInTableView method and it's always returning the correct number (2 in this example) 我将debug放入numberOfSectionsInTableView方法中,并且它始终返回正确的数字(在此示例中为2)

I've put debug in the cellForRowAtIndexPath, from which I've learnt: 1) If I drag the table view up high enough (so the cells disappear off the screen) and let go again, in circumstances when the issue is showing, it doesn't call cellForRowAtIndexPath again to try and redraw the cells when the table scrolls back into view, but when the issue is not there (ie the cells are all visible) it does call cellForRowAtIndexPath again for each cell. 我将debug放在cellForRowAtIndexPath中,从中学到了:1)如果我将表格视图拖动到足够高的位置(因此单元格从屏幕上消失了),然后在问题显示的情况下再次放开它,当表滚动回视图时,不会再次调用cellForRowAtIndexPath尝试重绘单元格,但是当问题不存在时(即,所有单元格都可见),它会为每个单元格再次调用cellForRowAtIndexPath。

2) At no point do any of the cells have the 'hidden' property true. 2)任何单元都绝对不具有“隐藏”属性。

From adding debug elsewhere (eg a temporary button) I can manually invoke the cellForRowAtIndexPath method for a cell which is not displayed, and it returns what seems to be a valid cell. 通过在其他地方添加调试(例如,临时按钮),我可以手动为未显示的单元格调用cellForRowAtIndexPath方法,并且它返回似乎有效的单元格。

So my question is, what can cause the issue I'm seeing? 所以我的问题是,什么会导致我看到的问题? I'm running out of places to put debug to see where the issue may be. 我没有足够的地方进行调试,以查看问题所在。

Would massively appreciate any help - what am I missing? 非常感谢您的帮助-我想念的是什么?

What's probably causing the issue is that you don't have enough cells to reuse. 可能导致此问题的原因是您没有足够的单元可重复使用。 Without seeing your code, I'm just speculating, but here's what can cause your issue: 我没有看到您的代码,只是在推测,但是这可能会导致您的问题:

Let's say your table is 100 points high and each cell is 10 points. 假设您的桌子高100点,每个单元格是10点。 Then your table, at any given time, has the 10 (or 11) cells that are currently in view in memory, as well as the 10 out of view above and 10 out of view below. 然后,在任何给定时间,您的表都会在内存中显示当前正在查看的10(或11)个单元格,以及上方的10个看不见的单元格和下方的10个看不见的单元格。 So, the table has about 30 cells that it creates and then reuses over and over again. 因此,该表具有大约30个创建的单元格,然后反复使用。

It seems that there aren't enough cells being created to be reused in some situations. 在某些情况下,似乎没有足够的单元格可重用。

Just a couple thoughts on how to "fix" the situation for you: 关于如何为您“解决”情况的几点想法:

  1. Use [myTableView reloadData] when the table view contentOffset.y changes by more than table view frame.size.height . 当表视图contentOffset.y变化大于表视图frame.size.height时,请使用[myTableView reloadData] This will call the cells in view to be recreated. 这将调用要重新创建的视图中的单元。 I do not recommend this method as it can be intense if there's a lot of data or a lot of cells that need to be recreated. 我不建议使用此方法,因为如果需要重新创建大量数据或大量单元,该方法可能会很激烈。
  2. Find how many cells are actually being created. 查找实际创建了多少个单元格。 You could add a tag to each cell and log each time a cell is brought into view. 您可以在每个单元格中添加标签,并在每次进入单元格时进行记录。 This might give you a better perspective on how cells are reused. 这可以使您更好地了解如何重用单元格。
  3. Depending on the number of cells that you need, you could create each cell, load it into an array, and then return a unique cell for cellForRowAtIndexPath . 根据所需的单元格数量,可以创建每个单元格,将其加载到数组中,然后为cellForRowAtIndexPath返回唯一的单元格。 Depending on the number of cells that you need, I think this might be the best option for you. 根据您所需的单元数,我认为这可能是您的最佳选择。

With help from Apple Dev Technical Support... I've resolved it. 在Apple Dev技术支持的帮助下,...已解决。 For the benefit of anyone else that comes across it, the reason was that I was accidentally calling the methods to reload the table from a secondary thread instead of on the main thread. 为了让遇到的其他人受益,原因是我不小心调用了从辅助线程而不是在主线程上重新加载表的方法。

To resolve, I used the following 为了解决这个问题,我使用了以下方法

[self performSelectorOnMainThread:@selector(refreshMethod) withObject:nil waitUntilDone:YES];

And so far so good. 到目前为止,一切都很好。

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

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