iphone - 滚动时 UITableView 未正确更新

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

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

表格加载正确,但滚动后 accessoryType 和 detailedTextLabel 混淆了。

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

最佳答案

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

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

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

https://stackoverflow.com/questions/4450366/

相关文章:

php - 如何在 PHP 中使用 substr_count() 计算空格

ruby-on-rails - Ruby on Rails 究竟是什么?

php - 如何在任何给定日期之前获得本周的周末?

opengl - 如何让鼠标光标在 opengl/glut 中消失?

hsqldb - 使用序列将 Liquibase 数据加载到 HSQLDB

c++11 - 无法创建 Direct2D DXGI 表面

sql-server - SQL 服务器 2008 : how does NOLOCK works

variables - Lua:为什么改变一个变量的值也会改变另一个变量的值?

php - 可以使用 PHP ImageMagick 获得透明渐变吗?

google-chrome - chrome.tabs 问题与 chrome.tabs.update