How to toggle the selected cell status of a UITableView
I have a UITableView with a custom cell, the cell contains a UIImageView and a UILabel. Now when I load the table for the first time, it is loaded with the same image per cell and different labels that they take from the LabelArray.
Now the image I'm talking about is a radioButton, so when the user clicks on the cell, the image changes. If the user clicks again, it goes to the default state.
To make this happen, I used this function and also declared a bool variable in the customcell class called selectionStatus.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell * cell = (CustomCell* )[tableView cellForRowAtIndexPath:indexPath];
if(indexPath.row == 0)
{
if(cell.selectionStatus == TRUE)
{
//Do your stuff
cell.selectionStatus = FALSE;
}
else
{
//Do your stuff
cell.selectionStatus = TRUE;
}
}
if(indexPath.row == 1)
{
if(cell.selectionStatus == TRUE)
{
//Do your stuff
cell.selectionStatus = FALSE;
}
else
{
//Do your stuff
cell.selectionStatus = TRUE;
}
}
}
This works great (but I want to know if this is the correct way or if we can check the cell.selected property) and I can get this effect. But now when I close the View and reopen it the function
Edit based on below comments with @Anil
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if([self.checkedIndexpath count] == 0)
{
[tableCell.selectionImage setImage:@"xyz.png"];
}
else
{
for (int i =0; i <[self.checkedIndexPath count]; i++)
{
NSIndexPath *path = [self.checkedIndexPath objectAtIndex:i];
if ([path isEqual:indexPath])
{
[tableCell.selectionImage setImage:@"abc.png"]
}
else
{
[tableCell.selectionImage setImage:@"xyz.png"]
}
}
return tableCell;
Ranjit's relationship
source to share
you need to store the pointer path to the selected row in the didSelectRowAtIndexPath file and check the index path in cellForRowAtIndexPath: set the corresponding image
Do you need multiple options? try it...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell * cell = (CustomCell* )[tableView cellForRowAtIndexPath:indexPath];
if(indexPath.row == 0)
{
if(cell.selectionStatus == YES)
{
[self.checkedIndexPaths addObject:indexPath];
//Do your stuff
cell.selectionStatus = NO;
}
else
{
[self.checkedIndexPaths removeObject:indexPath];
//Do your stuff
cell.selectionStatus = YES;
}
}
}
Edit
In cellForIndexPath check this like
// Set the default image for the cell. imageXYZ
for (NSIndexPath *path in self.checkedIndexPath)
{
if ([path isEqual:indexPath])
{
//set the changed image for the cell. imageABC
}
// no need of else part
}
Will we see exactly
source to share
Another option to toggle UItableViewCells ...
In willSelectRowAtIndexPath returns nil for NSIndexPath if the cell is already selected, also set the instance variable _selectedIndexPath.
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
if (cell.selected) {
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
[self.tableView.delegate tableView:self.tableView didDeselectRowAtIndexPath:indexPath];
indexPath = nil;
}
_selectedIndexPath = indexPath;
return indexPath;
}
The didSelectRowAtIndexPath and didDeselectRowAtIndexPath update your cell based on the cell.selected property ...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
if (cell.selected) {
// do stuff
} else {
// do stuff
}
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
if (cell.selected) {
// do stuff
} else {
// do stuff
};
}
and finally, in viewDidLoad, set the clearsSelectionOnViewWillAppear property ...
- (void)viewDidLoad {
[super viewDidLoad];
self.clearsSelectionOnViewWillAppear = NO;
}
source to share
Take a boolean in the model class and update it on the didSelectRow method and reload the table. In cellForRowAtIndexPath check it like this.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
After making your cell check like that
ModelClass *yourModel=[self.yourArray objectAtIndex:indexPath.row];
if(yourModel.selection)
[cell.customImage setImage:[UIImage imageName:@"selected"];
else
[cell.customImage setImage:[UIImage imageName:@"un_selected"];
}
Check this link https://github.com/adnanAhmad786/Quiz-View--TableView
source to share