2013-03-11 137 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]; 
}