0

当滚动我的 UITableView 时(往往是当我快速滚动它时),我的单元格的数据会混淆,因此标签可能会重复等。

我知道重复使用单元格可能会导致这种情况,但是如果用户真的快速向下滚动列表并且所有单元格都混淆了,我应该如何避免这种情况?

谢谢。

- (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;
}
4

5 回答 5

2

您通常使用这种代码:

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

fill cell with actual data from current row
return cell;

如果你将代码“用当前行的实际数据填充单元格”移动到“if”中——你会得到你现在得到的那种行为。

所以答案将是“在初始化后用数据填充单元格,在“if(cell == nil)”块之外。

于 2012-12-21T09:51:51.957 回答
0

在您的自定义单元类覆盖prepareForReuse方法中。在此方法中,将标签的文本设置为 nil,并将 imageview 的图像也设置为 nil。每次重用单元格时都会调用此函数,因此您的问题将由此解决。可能是这样的

- (void)prepareForReuse{
    [super prepareForReuse];
    self.titleLabel.text = nil;
    self.unitImageView.image = nil;
}
于 2012-12-21T11:24:09.210 回答
0

设置dequeueReusableCellWithIdentifiernil例如..

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];

更新:

请参阅此示例...我还使用自定义 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;
}

希望这对你有帮助....

于 2012-12-21T09:53:41.563 回答
0

UITableView如果单元格所在的位置当前不在屏幕上,则只会将单元格出列以供重复使用。因此,您不必担心“混淆”。

于 2012-12-21T09:51:41.847 回答
0
static NSString *cellIdentifier=@"cell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell==nil)
    {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    }

我想这会对你有所帮助。

于 2012-12-21T09:53:13.487 回答