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 cellForRowAtIndex开发者_如何学运维Path:indexPath];
UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[activityView startAnimating];
cell.accessoryView = activityView;
[activityView release];
}
But this isn't working. Any ideas?
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];
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;
精彩评论