CellForRowAtIndexPath returning the wrong cell

北战南征 提交于 2019-12-13 21:41:14

问题


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.

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).

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

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


回答1:


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.



来源:https://stackoverflow.com/questions/14084833/cellforrowatindexpath-returning-the-wrong-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!