【问题标题】:iOS: UITableView mixes up data when scrolling too fastiOS:UITableView 在滚动太快时混淆数据
【发布时间】:2012-09-03 23:17:38
【问题描述】:

我对 UITableViewCell 进行了子类化,以便为其添加自定义外观。在 MYTableViewCell 的初始级别,我添加了 4 个子视图:UIImageView 和三个 UILabel(s)。所有 4 个子视图都分配有不同的标签。

在 cellForRowAtIndexPath 方法中,如果一开始不可用,我要么创建一个新单元格,要么重用可用的单元格并将正确的文本分配给 ui 标签。

我遇到的问题是,如果我尝试超快滚动,那么数据就会变得混乱,但是如果我上下滚动更慢,那么一切正常。

有什么想法吗??

下面是代码:

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

DisplayableEntity *displayableEntity = [self.fetchedResultsController objectAtIndexPath:indexPath];

if( ! cell ) {
    cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    [self tableView:tableView appearanceForCell:cell withEntity:displayableEntity];
} else {

    UIImageView *imageView = (UIImageView *) [cell viewWithTag:IMAGEVIEW_TAG];
    imageView.image = [UIImage imageNamed:displayableEntity.displayImageName];

    UILabel *titleLabel = (UILabel *) [cell viewWithTag:TITLEVIEW_TAG];
    titleLabel.text = displayableEntity.entityName;

    UILabel *itemDescription = (UILabel *) [cell viewWithTag:DESCRIPTION_TAG];
    itemDescription.text = displayableEntity.entityDesctiption;
  }
}

// some code removed to make it brief
- (void)tableView:(UITableView *)tableView appearanceForCell:(MyTableViewCell *)cell withEntity:(DisplayableEntity *)entity {

    // cell image view
    UIImageView *cellImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[entity displayImageName]]];
    [cellImageView setTag:IMAGEVIEW_TAG];
    [cell addSubview:cellImageView];

    // adding entity name label    
    UILabel *itemTitleName = [self itemTitleNameLabelWithFrame:itemNameLabelRect itemName:[entity entityName]];
    [itemTitleName setTag:TITLEVIEW_TAG];
    [cell addSubview:itemTitleName];

    // adding 'assigned to' label right under the item name label
    UILabel *itemDescriptionLabel = [self itemDescriptionLabelWithFrame:descriptionLabelFrame itemDescription:[entity entityDesctiption]];
    [itemDescriptionLabel setTag:DESCRIPTION_TAG];
    [cell addSubview:itemDescriptionLabel];
}

【问题讨论】:

    标签: ios uitableview reuseidentifier


    【解决方案1】:

    我发现tableView:cellForRowAtIndexPath: 逻辑有些问题 应该是:

    1. 出列单元格
    2. 如果单元格不能出队 - 创建新的
    3. 设置所有单元格属性

    我的意思是这样的:

    - (MyTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        static NSString *CellIdentifier = @"itemListTableViewCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        } // <-- Note there is no else, we should reset properties in both cases
    
        NSManagedObject *managedObject = [_fetchedResultsController objectAtIndexPath:indexPath];
    
        cell.textLabel.text = [managedObject valueForKey:@"text"];
        cell.imageView.image = [managedObject valueForKey:@"image"];
    
        return cell;
    }
    

    【讨论】:

    • 谢谢!不知道为什么,但这解决了我的问题。在我的 if 子句中,我确实设置了单元格属性以及单元格外观,不知何故这真的把一切都搞砸了。一旦我删除了它,也删除了 else 子句,并且只在一个地方设置属性,就像你在你的例子中那样......然后一切都神奇地起作用了。再次感谢。
    • 为所有条件设置所有数据对我有用,谢谢
    猜你喜欢
    • 2018-06-14
    • 1970-01-01
    • 2013-01-04
    • 1970-01-01
    • 2016-10-13
    • 2021-05-27
    • 1970-01-01
    • 2021-05-22
    • 1970-01-01
    相关资源
    最近更新 更多