简体   繁体   中英

UITableView-didSelectRowAtIndexPath Problems when call didSelectRowAtIndexPath

I have a UITableView with CustomCell, in my CustomCell I added UIImage , UILabel ... But, when I selected cell in row 1 and remove UIImage on it : [[cell.contentView viewWithTag:TAG_FOR_TOPIMAG]removeFromSuperview]; However, cell at index 7 was changed same cell 1.

Table view reuses cells (dequeueReusableCell...).
So, the same cell (view) may be used for more than 1 index in the table.
After you change a cell's layout (add/remove/modify subviews) then this cell (view) will appear modified each time it is presented on the screen - even if it represents different index each time.

For example, in your case you probably have maximum of 6 cells visible in the same time on the screen.
Therefore, you have modified some cell (it was displayed for index 1 once you have modified it), after that you have scrolled the table, the cell with index 1 has disappeared from the screen, then the cell with index 7 should be presented (during the scrolling). The table will use the same cell (exactly the same UITableViewCell object) that was used for displaying row with index 1.
But you have removed the image from it...

You have to keep it in mind all the time when you work with tables.
In your case I would suggest you to set the visibility of the image view to hidden and restore it in each tableView"cellForRowAtIndexPath: method (NOT inside the if (cell == nil) statement - after it).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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