如何在tableview中选择新行时取消选择所选行?

时间:2013-01-26 06:34:24

标签: iphone ios ipad

我有一个应用程序,当用户选择新单元格时,我需要取消选择所选单元格。我可以选择和取消选择tableview行,但问题是我需要一次只选择一行。我现在这样做了,

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
   cell.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"white_cell_check.png"]];   

}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
      UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

     cell.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"white_cell.png"]]; 

}

但在此我可以选择并取消选择同一行。但我的意图是在选择新行时,如果已选择任何其他单元格,则需要取消选择。任何人都可以帮助我吗?

6 个答案:

答案 0 :(得分:6)

一个天真的解决方案是

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  for (int i = 0; i < [tableView numberOfRowsInSection:0]; i++) {
      [tableView deselectRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
  } 
   UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
   cell.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"white_cell_check.png"]];   

}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
      UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

     cell.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"white_cell.png"]]; 

}

基本上,当您选择一个新单元时,您将遍历所有单元格,取消选择它们。

答案 1 :(得分:1)

使用:

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

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

答案 2 :(得分:0)

您可以使用deselectRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    {
          [tableView deselectRowAtIndexPath:indexPath animated:YES];

    }

答案 3 :(得分:0)

尝试下面的代码,希望它可以帮到你。

int rowNumber = [tableCategory numberOfRowsInSection:0];

for (int indexRow = 0 ; indexRow < rowNumber; indexRow++) {

[NSIndexPath indexPathForRow:indexRow inSection:0];

[tableCategory cellForRowAtIndexPath:indexPath] ;

<required indexpath row>)

cell

cell

}

答案 4 :(得分:0)

尝试以下方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  for (int i = 0; i < [tableView numberOfRowsInSection:0]; i++) {
    [tableView deselectRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
    cell.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"white_cell.png"]]; 
  }

   cell.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"white_cell_check.png"]];   
}

答案 5 :(得分:0)

我有这样的选择处理。我假设在表视图中有两个组,其中有一个选项可以单独选择一个选项,即0,1个部分是一个单选的组, 2节是另一组单选 在Storbyboard中我选择了“Multiple Selection”但是在这里我取消选择给定组中的其他行。

  // MARK: - OPTIONS SELECTION HANDLING
    func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {

        print("Will select row \(indexPath.row) in section \(indexPath.section)")

        switch indexPath.section {
        case 0: fallthrough
        case 1:
            // Package Options
            // unselect other packages
            tableView.indexPathsForSelectedRows?.forEach({ (indexPath) in
                if indexPath.section == 0 || indexPath.section == 1 {
                    tableView.deselectRow(at: indexPath, animated: false)
                }
            })
        case 2:
            // A'la carte Cleaning Options
            // unselect other cleanings
            tableView.indexPathsForSelectedRows?.forEach({ (indexPath) in
                if indexPath.section == 2 {
                    tableView.deselectRow(at: indexPath, animated: false)
                }
            })
          default:
             fatalError("Impossible section number (\(section))!")
        }

        return indexPath
    }
相关问题