NSUser
NSUser

Reputation: 1233

How to revert back the changes made in UITableViewCell

I have a custom UITableViewCell named as Hobbies.

Everything is working fine.Except one UIIssue.

When user taps on any cell I want to change the text colour of that particular Cell .And when user select another I want the previous selected cell should return to its original state.

Currently I am able to change the colour on Cell select but not able to revert it back when user selects another.

Here is the code I am using to change the textColor of a particular cell:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 HobbiesCell *cell = (HobbiesCell*)[tableView cellForRowAtIndexPath:indexPath]; 
cell.dateLabel.textColor = [UIColor colorWithWhite:255 alpha:0.5f];

}

How can I revert It back when user selects another cell.

Upvotes: 2

Views: 223

Answers (6)

Rashmi Ranjan mallick
Rashmi Ranjan mallick

Reputation: 6630

Try cell.textLabel.highlightedTextColor.

    if (cell == nil) {

        ........
         /* your cell initiation code */
        ........

        cell.textLabel.textColor = [UIColor whiteColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.highlightedTextColor = [UIColor orangeColor];
    }

Upvotes: 1

Michael Platt
Michael Platt

Reputation: 526

You need to maintain a reference to the currently selected indexPath. Something like...

@property (nonatomic, strong) NSIndexPath *currentHobby;

Then in your didSelectRowAtIndexPath: method insert this...

if(_currentHobby && ![_currentHobby isEqual:indexPath]) {
    HobbiesCell *currentCell = [tableView cellForRowAtIndexPath:_currentHobby];
    currentCell.dateLabel.textColor = [UIColor originalColor];
}
_currentHobby = indexPath;

Then in your cellForRowAtIndexPath: make sure you include...

if([indexPath isEqual:_currentHobby]) {
    cell.dateLabel.textColor = [UIColor selectedColor];
} else { 
    cell.dateLabel.textColor = [UIColor originalColor];
}

Upvotes: -2

Garry
Garry

Reputation: 407

If you go for reload the tableview then try this .

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  // reload the table 
  [tableView reloadData];
 HobbiesCell *cell = (HobbiesCell*)[tableView cellForRowAtIndexPath:indexPath];
previousCell.dateLabel.textColor = [UIColor colorWithWhite:255 alpha:0.5f]; // color insert which you want to insert 
}

hope it helps you without adding varible.

Upvotes: 1

user623396
user623396

Reputation: 1547

You should always keep in mind that the cells are reusable, so the one you change will be used as is for displaying other rows when you scroll.

Instead, you should keep an array of your own models that keep the data (in your case color information) and use the cells only for displaying it.

To revert the color simply keep a reference to the latest NSIndexPath.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     MyRowModel *prevRowModel = [self.rowModels objectAtIndex:self.lastIndexPath.row];
     prevRowModel.color = [UIColor colorWithWhite:255 alpha:1f];

     MyRowModel *rowModel = [self.rowModels objectAtIndex:indexPath.row];
     rowModel.color = [UIColor colorWithWhite:255 alpha:0.5f]; 

     [self.tableView reloadRowsAtIndexPaths:@[indexPath, self.lastIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
     self.lastIndexPath = indexPath;
}

Upvotes: 1

Milap Kundalia
Milap Kundalia

Reputation: 1606

declare this variable

int selectedIndex;

in your cellForRowAtIndexPath

if(indexPath.row == selectedIndex)
{
cell.dateLabel.textColor = [UIColor colorWithWhite:255 alpha:0.5f];
}
else
{
    cell.dateLabel.textColor = [UIColor colorWithWhite:0 alpha:0.5f];//your default cell color
}

and in your didSelectRowAtIndex

selectedIndex = indexPath.row;
[tableView reloadData];

Upvotes: 1

Gandalf
Gandalf

Reputation: 2417

You can create an object of UITableViewCell say previousCell and assign it each time you select one. This will be your last selected cell and you can assign it the default color each time you click a new cell.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HobbiesCell *cell = (HobbiesCell*)[tableView cellForRowAtIndexPath:indexPath];
    previousCell.dateLabel.textColor = [UIColor blackColor]; //Assuming that this is the color you want to go back 
    cell.dateLabel.textColor = [UIColor colorWithWhite:255 alpha:0.5f];
    previousCell = cell;
} 

Upvotes: 2

Related Questions