UITableView not updating correctly when scrolling

安稳与你 提交于 2019-12-24 17:53:25

问题


I am writing a UITableView that contains 2 sections. When the table first loads all the cells are displaying the correct info, but when I start scrolling up and down, cells detailedTextLabel and accessoryType are being refreshed incorrectly, such that some cells that should only contain a detailedTextLabel also contain an accessory, and cells that should only contain an accessory also contain a detailedTextLabel.

Inside cellForRowAtIndexPath: I am using nested switch/case statements to apply the correct values to the cells in their respective section/row. As far as i can tell the logic in these statements is correct, so is it possible that the value of cell variable is inccorect when updating?

The table loads correctly but after scrolling accessoryType and detailedTextLabel gets mixed up.

Click for link to screen shots of the table.

Here is the code inside of my UITableViewController subclass:

- (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:


Due to cell re-use, cells with previously set values re-appear.

Before the switch (indexPath.section) ..., initialize the detailTextLabel and accessoryType to defaults:

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



回答2:


Try to enclose the code in each switch cases inside parantheisis. So the above code looks like this:

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

I am not sure whether this work. Do inform if it does :)



来源:https://stackoverflow.com/questions/4450366/uitableview-not-updating-correctly-when-scrolling

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