【问题标题】:How to deselect a selected row when selecting the new row in a tableview?在表格视图中选择新行时如何取消选择选定的行?
【发布时间】:2013-01-26 06:34:23
【问题描述】:

我有一个应用程序,当用户选择新单元格时,我需要在其中取消选择选定的单元格。我可以选择和取消选择 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"]]; 

}

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

【问题讨论】:

标签: iphone ios ipad


【解决方案1】:

一个天真的解决方案是

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

}

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

【讨论】:

  • 似乎不起作用..在这种情况下,当我选择每一行时,每个单元格背景都变为 white_cell_check.png。然后根本没有取消选择。它是一个分组表视图。只有一个部分
  • 通过添加 UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]]; cell.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"white_cell.png"]];进入循环就可以了。但是我需要双击才能更改单元格的背景
【解决方案2】:

使用:

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

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

【讨论】:

  • 对不起,现在所有 r 选择......表示我正在选择一行,其单元格图像已更改。而不是取消选择。再次选择时。
  • 这将取消选择选定的行。 OP想要取消选择所有其他人
  • @GabrielePetronella :那么我不确定什么应该是最好的解决方案,应该迭代所有行并选择除当前行之外的行。
  • @AKV 我在选择和取消选择时正在更改单元格图像,请注意
  • @hacker : 遍历所有并取消选择除当前之外的所有内容怎么样?
【解决方案3】:

你可以使用deselectRowAtIndexPath

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

    }

【讨论】:

  • 和我的回答有什么不同吗?? :)
【解决方案4】:

试试下面的代码,希望对你有用。

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

}

【讨论】:

【解决方案5】:

试试下面的一个:

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

【讨论】:

    【解决方案6】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-09
      • 1970-01-01
      • 2018-05-06
      相关资源
      最近更新 更多