views:

43

answers:

2

What's the best way of adding an image for the touch state in a UITableViewCell?

I know how to set the cell.selectionStyle but I actually need to add an image (a different one for each row) just when the user is touching it (and it pushes the new view in) Like a "Button State Touched".

What's the best way of achieving this? I tried with

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { }

Or would I have to do it inside

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

Thanks!

+1  A: 

I recommend that you create a custom UITableViewCell and use the touchesBegan and touchesEnded.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchedBegan:touched withEvent:event];
    // ... display image 2, hide image 1
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchedEnded:touched withEvent:event];
    // ... display image 1, hide image 2
}
jojaba
+1  A: 

I think you need both:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

For the didSelectRow, you need to store the current selected cell index. Then you call [tableView reloadData]

Then in the cellForRowAtIndexPath:, you need to set cell.imageView.image = [UIImage imageNamed:@"your_image_here"];

vodkhang