重置UITableViewCell复选标记用于按钮操作

时间:2016-12-14 09:21:03

标签: ios objective-c xcode uitableview

以下代码以空白单元格开头,并在我选择单元格时添加复选标记但是,我想在外部点击按钮时删除所有复选标记。你能建议我怎么实现这个?

a.modPow(b, n1) == 32

3 个答案:

答案 0 :(得分:1)

在按钮的IBAction中重新加载表格并在cellForRowAtIndexPath中写入cell.accessoryType = UITableViewCellAccessoryNone;

答案 1 :(得分:0)

如果您是新手,最简单的解决方案是管理所选索引的NSMutableArray。

NSMutableArray * selectedArray = [NSMutableArray new];

在每个选择中选择检查阵列中是否已存在选定的索引路径?

 - (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];

答案 2 :(得分: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:
            //labelInfo.text=@"2";
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            break;
            case 2:
            //labelInfo.text=@"3";
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            break;
            case 3:
            //labelInfo.text=@"4";
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            break;
            case 4:
            //labelInfo.text=@"5";
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            break;
            default:
            break;
        }

        previousClickedIndex = indexPath.row;
    }
}
相关问题