Changing color of UITableViewCell on state pressed and state selected

Akbari Dipali picture Akbari Dipali · Mar 4, 2013 · Viewed 8.2k times · Source

I am having UITableView with customCell(CustomCell having 2 lables and One ImageView).

In normal Mode I need white color of all the cells.

If user press the particular cell, then color of that cell should be gray(rest of the cells should be white)

And If user release the same cell, Color of that cell should be Orange(rest of the cells should be white)

How can I figure it out?

I have tried this using setSelectedBackground, willSelectRowAtIndexPath and Gestures methods. But can't see these 3 Color states for the same Cell. Any Of the 2 states are working together.

Any Idea how can i achieve the same functionality?

I have implemented same functionality in android using selectors. I want the same functionality in iPhone. Any Help?

Thanks in advance!

Answer

Anil Kothari picture Anil Kothari · Mar 4, 2013

Write these two method in your Custom Cell

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    self.contentView.backgroundColor=[UIColor greenColor];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    self.contentView.backgroundColor=[UIColor orangeColor];

}