在行选择上更改UITableView accessoryView

时间:2011-03-10 08:20:09

标签: iphone objective-c ios uitableview

当我选择行时,我正在尝试更改UITableViewCell的附件视图,我有以下代码:

- (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];
}

但这不起作用。有什么想法吗?

2 个答案:

答案 0 :(得分:8)

您不应该向tableview数据源(您的自己)询问单元格。因为这会创建一个刚刚不显示的新单元格。

替换

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

UITableViewCell *cell = [aTableView cellForRowAtIndexPath:indexPath];

答案 1 :(得分:5)

    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;