CellForRowAtIndexPath返回错误的单元格 [英] CellForRowAtIndexPath returning the wrong cell

查看:60
本文介绍了CellForRowAtIndexPath返回错误的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个问题,其中cellForRowAtIndexPath返回错误的单元格,因此,它正在隔离并在错误的单元格上执行其他操作.

I am running into an issue where cellForRowAtIndexPath is returning the wrong cell and therefore, it is seguing and performing other actions on the wrong cell.

我建立了相当大的自定义单元,因为我正在安装一些UIImageViews(+文本+按钮),所以一个单元占据了整个屏幕.当您向下滚动并同时在屏幕上显示两个单元格时,如果您点击顶部的一个单元格,则会选择底部的单元格内容(单元格内的按钮操作相同).

I have built custom cells that are quite big because I am fitting some UIImageViews (+text +buttons), so a cell takes up the entire screen. When you scroll down and two cells are on the screen at the same time, if you tap on the top one, it segues to the content of the bottom one (same for button actions inside the cell).

我认为这可能是特定于IB的,所以我用代码重建了所有内容,但是仍然有相同的行为.

I thought this might be specific to IB, so I rebuilt everything in code, but am still having the same behavior.

有什么办法可以解决此问题并维护正确的indexPath?

Is there any way to fix this and maintain the correct indexPaths?

推荐答案

我认为您的观点有误.cellForRowAtIndexPath不会返回错误的单元格.它应该只返回一个新的单元格,如果您使用的是dequeueReusableCellWithIdentifier或类似的单元格,则该单元格可能已经被回收了.在这种情况下,dequeueReusableCellWithIdentifier将返回所有可能刚刚滚动到顶部的旧单元格.取出新单元格(可以是新单元格或回收单元格),然后将正确的值填充到cellForRowAtIndexPath中,这是您的工作.然后,它返回的单元格将是正确的"单元格,因为您已经为其写入了所有正确的值.因此,我将查看您对cellForRowAtIndexPath的实现,以确保将传递的索引路径的正确值填充到cellForRowAtIndexPath返回的单元格中.

I think you have your perspective wrong. cellForRowAtIndexPath doesn't return wrong cells. It should just return a fresh cell, which may have been recycled if you're using dequeueReusableCellWithIdentifier or similar. In which case, dequeueReusableCellWithIdentifier will return any old cell which may have just scrolled off the top. It's your job to take the fresh cell (which may be a new cell or a recycled one) and stuff the correct values in it, inside cellForRowAtIndexPath. Then the cell it returns will be the "correct" one because you've just written all the correct values to it. So I would look at your implementation of cellForRowAtIndexPath to make sure that correct values for the index path that you're passed are stuffed into the cell that cellForRowAtIndexPath returns.

这篇关于CellForRowAtIndexPath返回错误的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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