在uitableviewcell中更改自定义accessoryView?

时间:2010-05-20 15:55:34

标签: iphone objective-c uitableview

我正在尝试在用户点击单元格后立即更改uitableviewcell的自定义accessoryView。我该怎么做?

为了记录,我正在使用Matt Gallagher的自定义表视图教程:

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

下载源代码链接:http://projectswithlove.com/projects/EasyCustomTable.zip


修改

尝试了这段代码,但它只是修改了配件AFTER touch-up。还尝试了selectSelectRowAtIndexPath,结果相同。

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

修改

通过使单元格的背景图像包含附件来解决问题。所以配件是“伪造”的,使它成为图像的一部分

3 个答案:

答案 0 :(得分:6)

也许您可以尝试在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];
 }

另一项建议: didSelectRowAtIndexPath 返回void 而不是NSIndexPath。

答案 1 :(得分:1)

您可以在选择方法中更改accessoryView,如下所示:

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

这只是抓取所选的当前单元格,然后更改它的附件视图。您的视图对象将在newView的位置。

答案 2 :(得分:0)

使用UIImageView的highlightImage属性:

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