Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios uitableview uitableviewcell reuse has old data

we all know that the cell in uitableview can be resued, but i am seeing some weird results. i am create a custom cell, each cell has 4 columns, each column has an image so format is like this |img|img|img|img| after the cell is returned, i am configuring each cell, an example would be configuring first column to picture 1, then second column to different picture

now just assuming that i can only display two rows at a time in the tableview:

|a0|a1|a2 |a3 |
|b4|b5|b6 |b7 |
------cell c not visible to user------
|c8|c9|c10|c11|

as user scrolls down, column 'a' (a0-a3) above, will become invisible, and as far i know that cell becomes invisible will be reused for column c

the example above works fine, if i can fully occupy every cell/row, i.e. i have total 12 pictures.

but if i have say 10 pictures (last row will be filled with only 2, and the other two are empty, like as the following:

|a0|a1|a2 |a3 |
|b4|b5|b6 |b7 |
------row/cell 'c' not visible to user------
|c8|c9|empty|empty|

however, problem arises, when i scroll up to see cell/row 'c' i have the following picture:

|a0|a1|a2 |a3 |
------row/cell 'a' not visible to user------
|b4|b5|b6 |b7 |
|c8|c9|a2|a3|

notice that the row/cell c's last two columns should be empty, but is showing a2 and a3 instead

how can i fix this? i am thinking about a manual way of doing this, i.e. by checking if i should remove unnecessary views etc when reusing, but could be more complicated is there a more systematic way of doing this that is easier and cleaner?

is there a way we could ask the returned cell to forget its content, or do i have to do this myself?

thanks

like image 892
user1118019 Avatar asked May 03 '12 23:05

user1118019


2 Answers

You have to "do it yourself". Whenever a UITableViewCell is reused, it retains its previous contents. If you want it to be reset/cleared, you have do this manually.

In your custom UITableViewCell, you can override prepareForReuse and reset any content you'd like or do it when setting the new content.

In your case, setting the UIImageView's image property to nil in all four columns should work.

like image 116
Andreas Ley Avatar answered Nov 14 '22 23:11

Andreas Ley


I had a similiar issue recently. Override prepareForReuse after subclass the tableview cell fixed it. Got the answer from here

like image 42
user523234 Avatar answered Nov 14 '22 23:11

user523234