Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 在tableview中选择新行时,如何取消选择选定行?_Iphone_Ios_Ipad - Fatal编程技术网

Iphone 在tableview中选择新行时,如何取消选择选定行?

Iphone 在tableview中选择新行时,如何取消选择选定行?,iphone,ios,ipad,Iphone,Ios,Ipad,我有一个应用程序,当用户选择一个新单元格时,我需要取消选择所选单元格。我可以选择和取消选择tableview行,但问题是一次只需要选择一行。我现在是这样做的 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; c

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

}
但在这里,我可以选择和取消选择同一行。但我的目的是在选择新行时,如果已经选择了任何其他单元格,则需要取消选择。有人能帮我吗?

使用:

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

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
您可以使用取消行索引路径


请尝试下面的代码,希望它能对您起作用

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

}

一个天真的解决办法是

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

}
基本上,只要您选择了一个新的单元格,您就可以在所有单元格中循环取消选择它们。

请尝试以下选项:

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

我有这样的选择处理。我假设表视图中有两个组具有单个选项选择,可以单独选择一个选项,即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
    }

与我的回答有什么不同吗??:抱歉,现在所有r选择……。表示我正在选择一行,其单元格图像已更改。未取消选择。再次选择时,这将取消选择所选行。OP希望取消选择所有others@GabrielePetronella:那么我不确定什么是最好的解决方案,shuold他迭代到所有行,并选择除当前行以外的所有行。@AKV在选择和取消选择时,我正在更改单元格图像,请注意that@hacker:循环浏览所有内容并取消选择除当前内容外的所有内容如何?抱歉,我目前在移动设备中,因此它的格式不正确。正确的代码段位于下面的链接中,我已经找到了该链接posted似乎不起作用..在这种情况下,当我选择每行时,每个单元格背景都变为白色\u cell\u check.png。然后根本没有取消选择。这是一个分组的表视图。通过添加UITableViewCell*单元格=[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i Instition:0]]只包含一个部分;cell.backgroundColor=[UIColor colorWithPatternImage:[UIImage ImageName:@white_cell.png];在循环中就可以了。但是我需要双击来改变单元格的背景,可能是重复的
  // 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
    }