简体   繁体   中英

Removing old data from reusable cell

I have a uitableview with each cell having a scroll view as the subview. the scrollview has a bunch of images in it. so when i change the data in the data source and after calling the reload table the images doesn't change but when i remove the dequeue the new data is reloaded. is there any method to remove the contents in the dequeue so that i don't get the old data

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"looser"];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault       reuseIdentifier:CellIdentifier] autorelease];
         scrollview=[[myscrollView alloc]initwitharray:imagearray];
         [cell.contentView addSubview:scrollview];
    }
}

A tableview works as follows:

It has room for a certain amount of cells on the screen, let's say 7 as an example. The tableview will ask you for the 7 cells of indexes 0 through 6.

If the top cell leaves the screen by scrolling, it will be placed in the reusable cell queue. There are now 6 cells on the tableview.

A new one comes up at the bottom now, the tableview asks for the cell at index 7. You call dequeueReusableCell, and you get the one that was at the top earlier.

The tableView has no idea what your cell is like, as it can be subclassed, so it will not make any changes to it. It is up to you to use your knowledge of how the tablecell is constructed to empty it, then fill it with the correct new data.

The reason tableview works like this is for performance. In stead of having maybe 100 views that would have to be checked (or mostly, ignored, which also costs time) for every scroll movement, it has a maximum of 7.

So in short, no. There are no default methods to remove data from reusable cells in UITableView, since UITableView can not and should not know what kind of cells they are. It is up to you to clear the cells when the tableview gives them to you.

Create a custom cell and it generates a method

- (void) prepareForReuse{}

Which do you cleanse all data from a cell and the output will be an empty cell.

也许您应该删除不需要的东西。

No, not while the cell is in the cache. When you dequeue a reusable cell you should clear out the old data first before using it again.

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