重置UItableViewCell复选标记 [英] Reset UItableViewCell checkmarks

查看:54
本文介绍了重置UItableViewCell复选标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码以空白单元格开头,并在选定单元格时添加一个选中标记,但是一旦添加,如果再次按该单元格,则不会将其删除.我试图每次调用didSelectRowAtIndexPath时将其重置.如何清除单元格以仅标记一个?谢谢.

following code starts with a blank cells and add a checkmark when selected cell but, once added, if you press again that cell, it is not erased. I am trying to reset it each time that didSelectRowAtIndexPath is called. How to clear cell in order to be marked only one? Thanks.

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

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

    if (indexPath.section == 0) {

    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;
        }
    }
}        

推荐答案

只需复制粘贴此内容即可.有用.

Just copy-paste this. It works.

  //in your .h file
   int selectedCell;

   //in your .m file
   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
   {

       NSString* cellIdentifier = @"cellIdentifier";
       UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

       if(cell == nil)
       {
           cell = (UITableViewCell*) [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];

       }
       cell.accessoryType = UITableViewCellAccessoryNone;

       if (indexPath.section == 0) {

           if(indexPath.row == selectedCell)
           {
                   cell.accessoryType = UITableViewCellAccessoryCheckmark; 
                   cell.selected = YES;
           }
           else
           {
               cell.accessoryType = UITableViewCellAccessoryNone;
               cell.selected = NO;
           }
       }
   }



   - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
   {
       selectedCell = indexPath.row;
       [tableView reloadData];
       //remaining code.
   }

这篇关于重置UItableViewCell复选标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆