滚动时UITableView无法正确更新

Sab*_*bin 1 iphone objective-c uitableview tableview

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

在内部cellForRowAtIndexPath:我使用嵌套的switch/case语句将正确的值应用于各自的section/row中的单元格.至于我可以说这些陈述中的逻辑是正确的,那么cell更新时变量的值是否可能是正确的?

该表正确加载但滚动后的accessoryType和detailedTextLabel混淆了.

单击以链接到表格的屏幕截图.

这是我的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;
}
Run Code Online (Sandbox Code Playgroud)

小智 8

由于细胞重复使用,具有先前设定值的细胞重新出现.

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

cell.detailTextLabel.text = @"";
cell.accessoryType = UITableViewCellAccessoryNone;
Run Code Online (Sandbox Code Playgroud)