Reputation: 8947
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 ?
Upvotes: 0
Views: 6088
Reputation: 11752
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
}
Upvotes: 0
Reputation: 78
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"]];
}
Upvotes: 0
Reputation: 935
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
}
Upvotes: 0
Reputation: 108101
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.
Upvotes: 6
Reputation: 17655
you can use deselectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Upvotes: 0
Reputation: 46533
Use :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Upvotes: 1