2013-03-11 77 views
1

我已经尝试了一切,以获得此工作,但当我滚动cellForRowAtIndexPath,它似乎是添加和删除我的tableview检查标记,任何人都可以帮助吗?UITableview复制复选标记

// Configure the cell... 
cell.textLabel.text = [currencyList objectAtIndex:indexPath.row]; 

// Check to see what currency is currently selected and checkstyle 
if ([selectedCurrency isEqualToString:cell.textLabel.text]) { 

    // Add a tick to the selected row 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 

    // Change the text colour 
    cell.textLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0.5 alpha:1]; 
    self.checkedIndexPath = indexPath; 
} else { 
    UITableViewCell *uncheckCell = [self.tableView 
            cellForRowAtIndexPath:self.checkedIndexPath]; 
    uncheckCell.accessoryType = UITableViewCellAccessoryNone; 
    // Change the font colour back to black 
    uncheckCell.textLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1]; 
} 

回答

1

您不需要取消选中cellForRowAtIndexPath中的单元格。更好的溶剂是重新加载检查单元格。

- (void)setCheckedIndexPath:(NSIndexPath *)indexPath 
{ 
    NSIndexPath *uncheckIndexPath = self.checkedIndexPath; 
    _checkedIndexPath = indexPath; 
    [self.tableView reloadRowsAtIndexPaths:@[uncheckIndexPath] 
          withRowAnimation:UITableViewRowAnimationLeft]; 
} 

并改变cellForRowAtIndexPath。请注意,我们将UITableViewCellAccessoryNone设置为我们必须返回的单元格,而不是您想要取消选中的单元格。

// Configure the cell... 
cell.textLabel.text = [currencyList objectAtIndex:indexPath.row]; 

// Check to see what currency is currently selected and checkstyle 
if ([selectedCurrency isEqualToString:cell.textLabel.text]) { 

    // Add a tick to the selected row 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 

    // Change the text colour 
    cell.textLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0.5 alpha:1]; 
    self.checkedIndexPath = indexPath; 
} else { 
    //If cell unchecked 
    cell.accessoryType = UITableViewCellAccessoryNone; 
    // Change the font colour back to black 
    cell.textLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1]; 
}