开发者

UITableView not updating correctly when scrolling

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;
}开发者_StackOverflow


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;


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜