Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change UITableView accessoryView on row select

I'm trying to change the accessory view of a UITableViewCell when its row is selected, I have the following code:

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell* cell = [self tableView:aTableView cellForRowAtIndexPath:indexPath];
    UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    [activityView startAnimating];
    cell.accessoryView = activityView;
    [activityView release];
}

But this isn't working. Any ideas?

like image 877
Paludis Avatar asked Mar 10 '11 08:03

Paludis


2 Answers

You should not ask the tableview datasource (your self) for a cell. because this will create a new cell which is just not displayed.

replace

UITableViewCell* cell = [self tableView:aTableView cellForRowAtIndexPath:indexPath];

with

UITableViewCell *cell = [aTableView cellForRowAtIndexPath:indexPath];
like image 195
Matthias Bauch Avatar answered Nov 15 '22 04:11

Matthias Bauch


    UIImage *normalImage = [UIImage imageNamed:@"cell-accessory.png"];
    UIImage *selectedImage = [UIImage imageNamed:@"cell-accessory-selected.png"];

    UIButton *accessoryView = [UIButton buttonWithType:UIButtonTypeCustom];
    accessoryView.frame = CGRectMake(0.0f, 0.0f, normalImage.size.width, normalImage.size.height);
    accessoryView.userInteractionEnabled = NO;
    [accessoryView setImage:normalImage forState:UIControlStateNormal];
    [accessoryView setImage:selectedImage forState:UIControlStateHighlighted];
    cell.accessoryView = accessoryView;
like image 31
Mr. Míng Avatar answered Nov 15 '22 04:11

Mr. Míng