2016-12-14 30 views
0

以下代码以空白单元格开头,并在选择单元格时添加复选标记,但我想在外部点击按钮时删除所有复选标记。你能建议我如何实现这一点?重置UITableViewCell复选标记对于按钮动作

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
     cell.accessoryType = UITableViewCellAccessoryNone; 

    if (indexPath.section == 0) { 

    switch (indexPath.row) { 
     case 0: 
      [email protected]"1"; 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      break; 
     case 1: 
      [email protected]"2"; 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      break; 
     case 2: 
      [email protected]"3"; 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      break; 
     case 3: 
      [email protected]"4"; 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      break; 
     case 4: 
      [email protected]"5"; 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      break; 
     default: 
      break; 
     } 
    } 
} 
+0

使用模型,例如属性'text'和'selected'。在'cellForRowAtIndexPath'中相应地设置标签文本和附件视图。要在**模型中更改某些内容**并重新加载行或表格视图。 – vadian

回答

1

在按钮的IBAction重新加载表和cellForRowAtIndexPathcell.accessoryType = UITableViewCellAccessoryNone;

0

如果你是新的最简单的解决方案将是管理选定索引的NSMutableArray。

NSMutableArray * selectedArray = [NSMutableArray new]; 

在每个Did选择检查选定的索引路径是否已经存在于您的数组?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

if([selectedArray containsObject:indexPath]){ 
//Your cell is already selected then unselect it 
[selectedArray removeObject:indexPath]; 

} 
else{ 
//Add object to indexpath 
    [selectedArray addObject:indexPath]; 
} 

[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]; 

} 

这是你的cellForRowAtIndexPath将

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

     UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"YOUR_ID" forIndexPath:indexPath]; 
     [cell setAccessoryType: UITableViewCellAccessoryNone]; 
     if ([selectedArray containsObject:indexPath]){ 
      [cell setAccessoryType: UITableViewCellAccessoryCheckmark]; 
     } 
     return cell; 

    } 

当你想清除所有复选标记从阵列中删除所有项目并重新加载细胞或

[YOUR_TABLEVIEW reloadRowsAtIndexPaths:[tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationNone]; 
0

尝试下面的代码,只需使用标志值更新以前单击的索引行。希望它可以帮助。

@interface ViewController() 
{ 
    int previousClickedIndex; //To save previous clicked row 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    previousClickedIndex = -1; //Initialize with a negative value 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    cell.accessoryType = UITableViewCellAccessoryNone; 

    if (indexPath.section == 0) { 

     if(previousClickedIndex>-1) 
     { 

      UITableViewCell *prevousSelectedCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:previousClickedIndex inSection:indexPath.section]]; 
      prevousSelectedCell.accessoryType = UITableViewCellAccessoryNone; 

     } 

     switch (indexPath.row) { 
      case 0: 
      //labelInfo.text=@"1"; 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      break; 
      case 1: 
      //[email protected]"2"; 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      break; 
      case 2: 
      //[email protected]"3"; 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      break; 
      case 3: 
      //[email protected]"4"; 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      break; 
      case 4: 
      //[email protected]"5"; 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      break; 
      default: 
      break; 
     } 

     previousClickedIndex = indexPath.row; 
    } 
}