简体   繁体   English

UITableView重用导致破碎的细胞

[英]UITableView Reuse Causing Broken Cells

When scrolling my UITableView (tends to be when I scroll it fast) the data for my cells gets mixed up, so labels might be repeated etc. 滚动我的UITableView时(往往是我快速滚动),我的单元格的数据会混淆,因此标签可能会重复等等。

I understand that reusing the cells probably causes this, but what if the user scrolls down the list really quickly and all the cells get mixed up, how am I supposed to avoid this? 我知道重复使用单元格可能会导致这种情况,但是如果用户真的很快地向下滚动列表并且所有单元格混淆了怎么办,我该如何避免这种情况呢?

Thanks. 谢谢。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"VideoListCell";
    VideoListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[VideoListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    if (isPlaylistView)
    {
        //Fill cell with data
    }
    else if (isPlaylistDetailView || isSearchResultsView)
    {
        //Fill cell with data
    }
    else
    {
        //Playlist button and uploads
        if (indexPath.section == 0)
        {
            //Fill cell with data
        }
        else
        {
            //Fill cell with data
        }
    }

    return cell;
}

You generally use this kind of code: 您通常使用这种代码:

cell = dequeReusableCell;
if (cell == nil) { 
    create cell;
    initialize cell;
}

fill cell with actual data from current row
return cell;

If you will move code "fill cell with actual data from current row" into "if" — you will get the kind of behavior you get right now. 如果您将代码“将当前行的实际数据填充单元格”移动到“if” - 您将获得您现在获得的行为。

So the answer will be "fill cell with data after you initialize it, outside of "if (cell == nil)" block. 因此答案将是“在初始化之后用”if(cell == nil)“块之外的数据填充单元格。

UITableView will ever only dequeue a cell for reuse if the position that the cell was in is currently off-screen. 如果单元所在的位置当前在屏幕外, UITableView将只会使单元格出列以便重复使用。 So you don't have to worry about "mix-ups". 所以你不必担心“混淆”。

static NSString *cellIdentifier=@"cell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell==nil)
    {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    }

I think it will be helpful to you. 我认为这对你有所帮助。

set dequeueReusableCellWithIdentifier to nil for example.. dequeueReusableCellWithIdentifier设置为nil例如..

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];

UPDATE: 更新:

See this Example... i load many data in the cell with also my custom Gridview... 看到这个例子......我在单元格中加载了很多数据,还有我的自定义Gridview ......

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

    UITableViewCell *gridCell = [tableView dequeueReusableCellWithIdentifier:nil];

    if(gridCell == nil)        
    {
        gridCell =  [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];


    }
    return gridCell;
}

hope this help you.... 希望这能帮到你....

In your custom cell class override prepareForReuse method. 在自定义单元格类中重写prepareForReuse方法。 In this method set text of your labels to nil and set imageview's image to nil also. 在此方法中,将标签的文本设置为nil,并将imageview的图像设置为nil。 This function is called everytime when a cell is reused so your problem will be solved by this. 每次重复使用单元格时都会调用此函数,因此您的问题将得到解决。 May be like this 可能是这样的

- (void)prepareForReuse{
    [super prepareForReuse];
    self.titleLabel.text = nil;
    self.unitImageView.image = nil;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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