“被重用的表格单元格的无索引路径"是什么意思?iOS 6/7 中的消息? [英] What is the meaning of the "no index path for table cell being reused" message in iOS 6/7?

查看:23
本文介绍了“被重用的表格单元格的无索引路径"是什么意思?iOS 6/7 中的消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从开始使用 iOS 6(以及 iOS 7)编译我的应用程序以来,我开始看到这条消息.我知道 UITableViews 管理单元格的方式在 iOS 6 中是不同的,但我不需要修改我的代码来让它继续工作.但我担心此消息可能会指向一些我尚未发现的潜在问题.任何人都可以发光吗?

Since starting to compile my app with iOS 6 (and since also iOS 7) I've started seeing this message. I know that the way that UITableViews go about managing cells is different in iOS 6 but I haven't needed to modify my code for it to keep working. But I'm concerned this message may point to some potential issue that I'm not yet seeing. Can anyone shed any light?

推荐答案

从 iOS 7 beta 5 开始,我开始在日志中显示此错误,包括在 iOS 7 GM/Release 版本中,但从未出现过发生在我的 iOS 6 或更早的 iOS 7 测试版应用程序中.经过大量试验,我找到了原因:

I started to get this error showing up in the log from iOS 7 beta 5 onwards, including in the iOS 7 GM/Release build, whilst never having had it happen in my app in iOS 6 or the earlier iOS 7 betas. After a lot of experimenting I found the cause:

我将 UITableViewCell 对象用于我的部分标题视图,并在 tableView:viewForHeaderInSection: 中返回它们.这似乎是一种常见的做法,尤其是在 iOS 5 之后,当使用界面生成器在 StoryBoard 中将部分标题视图设计为原型表视图单元格变得很容易.

I was using UITableViewCell objects for my section header views and returning them in tableView:viewForHeaderInSection:. This appears to be common practice, especially since iOS 5 when it became easy to design a section header view as a prototype table view cell in a StoryBoard with Interface Builder.

当我将我的应用更改为仅对部分标题视图使用常规 UIView 子类时,错误消失了,更重要的是,我的表格视图停止随机删除部分标题!

When I changed my app to use just regular UIView subclasses for my section header views, the errors went away and, more importantly, my table view stopped randomly deleting section headers!

看起来(自 iOS 7 beta 5 起)UITableView 在内部维护其视图层次结构中所有 UITableViewCell 对象及其各自索引路径的映射.由于节标题(或页脚的表视图页眉)没有索引路径,如果您为这些视图使用 UITableViewCell 对象,则表视图在找到 UITableViewCell 它没有索引路径,导致没有索引路径被重用的表格单元格"错误,如果你不走运,在你的表格视图中显示故障:

It would appear that (since iOS 7 beta 5) UITableView is internally maintaining a mapping of all the UITableViewCell objects in its view hierarchy and their respective index paths. Since a section header (or a table view header of footer) doesn't have an index path, if you use a UITableViewCell object for these views, the table view will get confused when it finds a UITableViewCell for which it doesn't have an index path, resulting in the "no index path for table cell being reused" error and, if you're unlucky, display glitches in your table view:

更新:如果您可以访问 Apple Dev Forums,这里是关于它的主题(我开始的):https://devforums.apple.com/message/882042#882042

UPDATE: if you have access to the Apple Dev Forums, here's the thread about it (which I started): https://devforums.apple.com/message/882042#882042

正如该线程中所建议的,如果您不想进行太多重构,可以在 UITableViewCell 周围创建一个 UIView 包装器并将其作为部分返回标题视图.

As suggested in that thread, if you don't want to re-factor much, you can create a UIView wrapper around your UITableViewCell and return that as the section header view.

UIView *view = [[UIView alloc] initWithFrame:[cell frame]];
[view addSubview:cell];

return view;

但是请注意,这种包装器"UIView 方法不能很好地与 AutoLayout 和设备旋转配合使用,因此我建议您对页眉和页脚单元格使用 UIView 子类,而不是答案主要部分中解释的 UITableViewCell 子类.

Note however that this "wrapper" UIView approach will not play well with AutoLayout and device rotation, so I suggest that you use a UIView subclass for header and footer cells, not a UITableViewCell subclass as explained in the main part of the answer.

这篇关于“被重用的表格单元格的无索引路径"是什么意思?iOS 6/7 中的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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