I have a UITableView with a custom Cell, the cell contains a UIImageView and a UILabel. Now When I load my table first time, It loads with a same image on each cell and different labels, which it takes from the LabelArray.
Now the image I am talking about is a radioButton, So when the user clicks the cell, the image changes. If user clicks again it changes to default state.
For this to happen, I have used this function and also I have declared a bool variable in my 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 fine, (but I want to know whether it is a proper way, or can we check the cell.selected property) and I am able to get that effect. But now when I close the View and open it again 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;
Regards Ranjit
you have to save the index path of the selected row in didSelectRowAtIndexPath and check the index path in cellForRowAtIndexPath: set the corresponding image
Do you want multiple selection? try this...
Edit
In cellForIndexPath check like this
Do the exact we will see
Another option for toggling UItableViewCells...
In the willSelectRowAtIndexPath return nil for NSIndexPath if cell is already selected, also set your _selectedIndexPath instance variable.
In didSelectRowAtIndexPath and didDeselectRowAtIndexPath update your cell based on the property cell.selected ...
and finally in viewDidLoad set the clearsSelectionOnViewWillAppear property...
Take a boolean in your model class and update it on didSelectRow method and reload table. In cellForRowAtIndexPath check this like that.
}
Check this link https://github.com/adnanAhmad786/Quiz-View--TableView
First of all for toggling purpose declare a bool value selectionStatus in tableview cell class like this:
@property(nonatomic)BOOL selectionStatus;
Declare a NSMutableArray to store the selected indexpaths (for multi-selection purpose)
self.checkedIndexPaths = [[NSMutableArray alloc] init];
In didSelectRowAtIndexPath
a. if Bool value selectionStatus is No Insert indexPath to NSMutableArray checkedIndexPaths and set cell.selectionStatus = YES
b. if Bool value selectionStatus is Yes Remove indexPath from NSMutableArray checkedIndexPaths and set cell.selectionStatus = NO
c. Reload tableView each time
Find the code below :
In cellForRowAtIndexPath
a. First set cell image to unchecked i.e xyz.png here
[tableCell.selectionImage setImage:[UIImage imageNamed:@"xyz.png"]];
b. Set image as checked for all index paths in NSMutable array self.checkedIndexPaths