iOS – How toggle UITableView Cell Select Status

I have a UITableView with a custom Cell, the cell contains a UI ImageView and a UILabel. Now when I load my table for the first time, it will be in each Load the same image on the cell and different labels, it is obtained from LabelArray.

Now I am talking about the image is a radioButton, so when the user clicks on the cell, the image will change. If When the user clicks again, it will change to the default status.

For this, I used this function, and I declared a bool variable named selectionStatus in the customcell class.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell * cell = (CustomCell* )[tableView cellForRowAtIndexPath:indexPath];

if(indexPath.row == 0)
{
if(cell.selectionStatus == TRUE)
{
//Do your stuff< br /> 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 is fine, (but I want to know if it is a correct way, or we can check the cell.selected property), I was able to get this effect. But now when When I close the View and open its function again

Use @Anil to edit according to the following comments

- (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
Rangit

You must 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 choices? Try this…

- (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
Check as follows in cellForIndexPath

// Set the default image for the cell. imageXYZ 
for (NSIndexPath *path in self.checkedIndexPath) < br />{
if ([path isEqual:indexPath])
{
//set the changed image for the cell. imageABC
}
// no need of else part
}

Indeed, we will see

I have a Define the UITableView of the Cell, the cell contains a UI ImageView and a UILabel. Now when I load my table for the first time, it will load the same image on each cell and different label, it gets it from LabelArray

Now the image I am talking about is a radioButton, so when the user clicks on the cell, the image will change. If the user clicks again, it will change to the default state.

< p>For this, I used this function, and I declared a bool variable named selectionStatus in the customcell class.

- (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 is good, (but I want to know if it is a correct way, or we can check the cell.selected property), I was able to get this effect. But now when I close the View and open it again When functioning

Use @Anil to edit according to the following comments

- (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
Rangit

You must 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 selections? Try this…

- (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
Check as follows in cellForIndexPath

// Set the default image for the cell. imageXYZ 
for (NSIndexPath *path in self.checkedIndexPath) < br />{
if ([path isEqual:indexPath])
{
//set the changed image for the cell. imageABC
}
// no need of else part
}

Indeed, we will see

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3893 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.