【问题标题】:Select only one cell UITableView只选择一个单元格 UITableView
【发布时间】:2012-06-09 10:51:22
【问题描述】:

我有一个包含许多单元格的表格视图。当我按下一个时,它旁边会出现一个勾号,但是当我选择另一个时,前一个上的勾号仍然存在,并且在当前单元格上添加了一个新的勾号。所以勾选了两个单元格,但一次只能勾选一个!!

我试过了,但它不起作用:

if (cell.selected = YES) {

[cell setSelected:NO animated:YES];
[cell setAccessoryType:UITableViewCellAccessoryNone];

}else if(cell.selected = NO){

    [cell setSelected: YES animated:YES];
    [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    }

【问题讨论】:

    标签: objective-c xcode


    【解决方案1】:
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    int newRow = [indexPath row];
    int oldRow = [lastIndexPath row];
    
    if (newRow != oldRow)
    {
            UITableViewCell *newCell = [tableView cellForRowAtIndexPath:
                                                                    indexPath];
            newCell.accessoryType = UITableViewCellAccessoryCheckmark;
    
            UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:
                                                                    lastIndexPath];
            oldCell.accessoryType = UITableViewCellAccessoryNone;
    
            lastIndexPath = indexPath;
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
        if (cell.accessoryType == UITableViewCellAccessoryNone) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
    
        } else {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
    

    【讨论】:

    • 第二个有效,但按新单元不会取消选择旧单元格,只需按两次即可。第一个还可以。单元格变为蓝色,然后再次变为白色,但在旧单元格上仍然存在滴答声!
    • 我不得不在 Suhaiyl 的回答中添加一行:“the lastindex is property(retain) NSIndexPath* lastIndexPath”,所以也感谢他
    • 我发现了一个问题。如果表格很长,有时某些单元格会保持选中状态。为什么?
    【解决方案2】:

    另一种选择:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        for (int i = 0; i<yourArray.count;i++)
        {
            if(i!=[indexPath row])
            {
                cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
                cell.accessoryType = UITableViewCellAccessoryNone;
            }
        }
        cell = [tableView cellForRowAtIndexPath:indexPath];
    
        if (cell.accessoryType == UITableViewCellAccessoryNone) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
    
        } else {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
    

    这实际上会遍历所有行并禁用它们。有点蛮力,但它有效。如果您想要一个不允许取消选择的版本,

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        for (int i = 0; i<yourArray.count;i++)
        {
            cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        cell = [tableView cellForRowAtIndexPath:indexPath];
    
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    

    【讨论】:

      【解决方案3】:

      Swift 中的快速修复:

       var lastIndexPath:NSIndexPath = NSIndexPath(forRow: 0, inSection: 0)
      override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
          let newRow = indexPath.row
          let oldRow = lastIndexPath.row
      
          if oldRow != newRow {
              tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = .Checkmark
              tableView.cellForRowAtIndexPath(lastIndexPath)?.accessoryType = .None
      
              lastIndexPath = indexPath
          }
      
          tableView.deselectRowAtIndexPath(indexPath, animated: true)
      }
      

      【讨论】:

        【解决方案4】:

        在tableviewdatasource中你需要遵循这个:

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            static NSString *CellIdentifier = @"Cell";
            UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        
            if(cell == nil )
            {
                cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
            }
            if ([indexPath compare:self.lastIndexPath] == NSOrderedSame) 
            {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
            } 
            else 
            {
                cell.accessoryType = UITableViewCellAccessoryNone;
            }
            return cell;
        }
        
        // UITableView Delegate Method
        -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
        {
            self.lastIndexPath = indexPath;
        
            [tableView reloadData];
        }
        

        lastindex 是属性(retain) NSIndexPath* lastIndexPath;

        【讨论】:

        • 而不是 [cell setSelected:NO animated:YES];你试过 [tableView deselectRowAtIndexPath:indexPath animated:YES]; ??
        • 嘿,看看这个链接:stackoverflow.com/questions/1750753/…希望这对你有帮助。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-18
        • 1970-01-01
        • 2011-01-01
        • 1970-01-01
        相关资源
        最近更新 更多