iOS:当滚动太快时,UITableView混合数据

我已经分类了UITableViewCell添加自定义外观。 在MYTableViewCell的init级别,我添加了4个子视图:UIImageView和三个UILabel(s)。 所有4个子视图都分配了不同的标签。

在cellForRowAtIndexPath方法里面,我要么创build一个新的单元格,如果它不是最初可用的,或者重用可用的单元格并将正确的文本分配给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]; } 

我在tableView:cellForRowAtIndexPath:看到一些麻烦tableView:cellForRowAtIndexPath:逻辑它应该是:

  1. 出队细胞
  2. 如果细胞不能出队 – 创build一个新的细胞
  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; }