简体   繁体   中英

How to deselect a selected row when selecting the new row in a tableview?

I have an application in which i need to deselect the selected cell when the user selecting a new cell. I can do the selecting and deselecting the tableview rows, but the problem is I need to select only one row at a time. I was doing like this now,

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

}

but in this i can select and deselect the same row. But my intention is when selecting a new row if any other cells are selected already it needs to deselected. Can any body help me ?

A naive solution would be

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

}

Basically you are looping through all cells deselecting them whenever you select a new one.

Use :

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

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

you can use deselectRowAtIndexPath

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

    }

Try the below code hope it may work you.

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

}

Try the below one:

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

I have such selection handling. I assume 2 groups in table view with single selection of options that enables separate selection of one options, ie 0,1 sections is one group with single selection, 2 section is another group with single selection In Storbyboard I have chosen "Multiple Selection" But here I deselect other rows in given group.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM