cannyboy
cannyboy

Reputation: 24426

Changing a custom accessoryView in a uitableviewcell?

I'm trying to change the custom accessoryView of a uitableviewcell immediately after the user clicks on the cell. How would I do this?

For the record, I'm using Matt Gallagher' custom table view tutorial:

http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html

Download link for source: http://projectswithlove.com/projects/EasyCustomTable.zip


EDIT

Tried this code, but it just changes the accessory AFTER touch-up. Also tried willSelectRowAtIndexPath with same result.

- (NSIndexPath *)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UIImage *indicatorImage = [UIImage imageNamed:@"indicatorSelected.png"];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryView = [[[UIImageView alloc] initWithImage:indicatorImage] autorelease];
    return indexPath;
 }

EDIT

Problem solved by making the background image of the cell contain the accessories. so the accessory was 'faked' by making it part of the image

Upvotes: 3

Views: 6440

Answers (3)

charliehorse55
charliehorse55

Reputation: 1990

Use the highlightedImage property of UIImageView:

UIImageView* arrowView = [[UIImageView alloc] initWithImage:normalImage];
arrowView.highlightedImage = selectedImage;
cell.accessoryView = arrowView;
[arrowView release];

Upvotes: 0

phse
phse

Reputation: 2841

Perhaps you can try to reload the cell in willSelectRowAtIndexPath/didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UIImage *indicatorImage = [UIImage imageNamed:@"indicatorSelected.png"];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryView = [[[UIImageView alloc] initWithImage:indicatorImage] autorelease];
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:NO];
 }

Another recommendation: didSelectRowAtIndexPath returns void not NSIndexPath.

Upvotes: 6

rickharrison
rickharrison

Reputation: 4846

You could change the accessoryView in the selection method as shown:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryView = newView;
}

This just grabs the current cell that was selected and then changes the accessory view of it. Your view object would go where newView is.

Upvotes: 1

Related Questions