动态更改UITableViewCell的附件视图图像

时间:2012-11-05 16:00:49

标签: objective-c ios cocoa-touch

我有一个CustomTableView Cell,它有一个附件视图。如何动态更改配件视图的图像?

4 个答案:

答案 0 :(得分:2)

将自定义按钮用于附件视图,例如..

//在附件视图上粘贴铅笔图标

UIButton* pencil = [UIButton buttonWithType:UIButtonTypeCustom];
[pencil setImage:[UIImage imageNamed:@"icon-pencil.gif"] forState:UIControlStateNormal];
pencil.frame = CGRectMake(0, 0, 15, 15);
pencil.userInteractionEnabled = YES;
[pencil addTarget:self action:@selector(didTapEditButton:)      forControlEvents:UIControlEventTouchDown];
cell.accessoryView = pencil; 

答案 1 :(得分:0)

不要使用accessoryView,只需放置另一个imageView并相应更改

答案 2 :(得分:0)

在自定义单元格的init方法中,您可以执行类似(假设ARC btw):

UIImage *customBtnImg = [UIImage imageNamed:@"custom_btn"];
UIButton *customBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, customBtnImg.size.width, customBtnImg.size.height)];
customBtn.backgroundColor = [UIColor clearColor];
[customBtn setBackgroundImage:customBtnImg forState:UIControlStateNormal];
self.accessoryView = customBtn;

答案 3 :(得分:0)

除了textColor和detailTextColor之外,对单元格的修改应该在willDisplayCell:方法而不是cellForRowAtIndexPath方法中完成, 您的问题的解决方案如下 -

 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cellforIndexPath:(NSIndexPath *)indexPath
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageName:@"image.png" forControlState:UIControlStateNormal];
    btn.frame = CGRectMake(0,0,28,28) ;

    cell.accessoryView = btn ;

}

这段代码肯定会有用!!