0

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 ?

Rob
  • 415,655
  • 72
  • 787
  • 1,044
hacker
  • 8,919
  • 12
  • 62
  • 108
  • possible duplicate of [How to remove a custom icon to the UITableViewCellAccessoryCheckmark when another table row is selected](http://stackoverflow.com/questions/14378196/how-to-remove-a-custom-icon-to-the-uitableviewcellaccessorycheckmark-when-anothe) – Gabriele Petronella Jan 26 '13 at 07:21

6 Answers6

6

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.

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
  • seems to be not working..In this case when i select each rows each cell background became white_cell_check.png.then there is no deselection at all.its a grouped table view.having only one section – hacker Jan 26 '13 at 07:02
  • by adding UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]]; cell.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"white_cell.png"]]; in to the loop will do the trick .But i need to double click inorder to change the background of the cell – hacker Jan 26 '13 at 07:47
1

Use :

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

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
0

you can use deselectRowAtIndexPath

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

    }
Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70
0

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

}
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
Exploring
  • 925
  • 6
  • 18
  • Sorry currently I'm in Mobile so it is not formatted properly. – Exploring Jan 26 '13 at 06:57
  • The proper code snippet is in the below link which I've posted http://stackoverflow.com/questions/14378196/how-to-remove-a-custom-icon-to-the-uitableviewcellaccessorycheckmark-when-anothe/14378737#14378737 – Exploring Jan 26 '13 at 07:04
  • http://stackoverflow.com/questions/14378196/how-to-remove-a-custom-icon-to-the-uitableviewcellaccessorycheckmark-when-anothe – Exploring Jan 26 '13 at 07:16
0

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"]];   
}
dwu
  • 78
  • 1
  • 4
0

I have such selection handling. I assume 2 groups in table view with single selection of options that enables separate selection of one options, i.e 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
    }
Michał Ziobro
  • 10,759
  • 11
  • 88
  • 143