滚动时 UITableView 未正确更新

Posted

技术标签:

【中文标题】滚动时 UITableView 未正确更新【英文标题】:UITableView not updating correctly when scrolling 【发布时间】:2010-12-15 13:26:44 【问题描述】:

我正在编写一个包含 2 个部分的 UITableView。当表格第一次加载时,所有单元格都显示正确的信息,但是当我开始向上和向下滚动时,单元格 detailTextLabel 和 accessoryType 被错误地刷新,因此一些应该只包含 detailTextLabel 的单元格也包含一个附件,而单元格应该只包含一个附件还包含一个详细的TextLabel。

cellForRowAtIndexPath: 内部,我正在使用嵌套的 switch/case 语句将正确的值应用于各自部分/行中的单元格。据我所知,这些语句中的逻辑是正确的,那么更新时cell 变量的值是否可能是错误的?

表格加载正确,但在滚动附件类型和详细文本标签后会混淆。

Click for link to screen shots of the table.

这是我的 UITableViewController 子类中的代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    return [sectionNames count];


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    // Return the number of rows in the section.
    NSArray *headingsSection = [cellTitles objectAtIndex:section];
    return [headingsSection count]; 


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
    return [sectionNames objectAtIndex:section];


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

    self.tableView.allowsSelection = YES;
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    

    cell.textLabel.text = [[cellTitles objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    cell.textLabel.textColor = [UIColor blackColor]; 
    cell.textLabel.font = [UIFont boldSystemFontOfSize:15]; 

    switch (indexPath.section) 
    case 0:
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%d%%", [[assistSettingsArray_glob objectAtIndex:indexPath.row] intValue]];
        break;
    case 1:
        switch (indexPath.row) 
        case 0:
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            break;
        case 1:
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            break;
        case 2:
            if (defaultAssistOn) 
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
             else 
                cell.accessoryType = UITableViewCellAccessoryNone;
            
            break;
        
        break;
    

    return cell;

【问题讨论】:

【参考方案1】:

由于单元格重复使用,具有先前设置值的单元格重新出现。

switch (indexPath.section) ...之前,将detailTextLabel和accessoryType初始化为默认值:

cell.detailTextLabel.text = @"";
cell.accessoryType = UITableViewCellAccessoryNone;

【讨论】:

此外,如果您决定将子视图添加到内容视图,则需要删除带有 [[[cell contentView] subviews] makeObjectsPerformSelector: @selector(removeFromSuperview)]; 之类的子视图 哦,太好了,完美解决了这个问题。非常感谢。 :)【参考方案2】:

尝试将代码包含在 parantheisis 内的每个 switch case 中。所以上面的代码是这样的:

switch (indexPath.section) 
   case 0: 
       cell.detailTextLabel.text = [NSString stringWithFormat:@"%d%%",
               [[assistSettingsArray_glob objectAtIndex:indexPath.row] intValue]];
       break;
     
   case 1: 
       switch (indexPath.row) 
          case 0: 
              cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
              break; 
         case 1: 
             cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
             break; 
         case 2: 
             if (defaultAssistOn) 
               cell.accessoryType = UITableViewCellAccessoryCheckmark;
            
            else
               cell.accessoryType = UITableViewCellAccessoryNone;
            
            break; 
        default:
            break;
       
       break;
     
    default:
    break;
  

我不确定这是否有效。如果有,请告知:)

【讨论】:

前面的回答其实解决了这个问题。感谢您的帮助。

以上是关于滚动时 UITableView 未正确更新的主要内容,如果未能解决你的问题,请参考以下文章

UITableView 单元格内容未正确显示

关闭另一个视图后 UITableView 滚动内容大小不正确

UITableView 滚动后以编程方式创建的 UITableViewCell 未正确显示

UITableCell 中的 UILabel 大小不正确,直到 UITableView 滚动

滚动/调整 UITableView

UITableView 仅在向上滚动时更新,而不是向下滚动