iOS: UITableView mixes up data when scrolling too fast

孤街浪徒 提交于 2019-12-23 13:11:15

问题


I've subclassed the UITableViewCell to add custom appearance to it. At the init level of the MYTableViewCell I added 4 subviews: UIImageView, and three UILabel(s). All 4 subviews have a different Tag assigned to them.

Inside the cellForRowAtIndexPath method I either create a new cell if it wasn't available at first, or reuse available one and assign the proper text to the ui labels.

The problem I am having is that if I try to scroll super fast, then the data gets messed up, however if I scroll up and down more slowly, then everything works fine.

Any thoughts??

Below is the code:

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

回答1:


I see some troubles in tableView:cellForRowAtIndexPath: logic It should be:

  1. Dequeue cell
  2. If cell cannot be dequeued - create the new one
  3. Set all cell properties

I mean something like this:

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


来源:https://stackoverflow.com/questions/12254988/ios-uitableview-mixes-up-data-when-scrolling-too-fast

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!