2010-05-20 95 views
2

我想在用户单击单元格后立即更改uitableviewcell的自定义accessoryView。我将如何做到这一点?更改uitableviewcell中的自定义accessoryView?

为了记录在案,我使用的是马特·加拉格尔”自定义表视图教程:

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

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


编辑

尝试这种代码,但它只是在触摸后更改附件。还试着用相同的结果willSelectRowAtIndexPath。

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

EDIT

问题通过使细胞的背景图像解决包含的附件​​。因此附件是“伪造”通过使图像的一部分

回答

1

你可以改变在选择方法的accessoryView的,如下所示:

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

这正好抓住了所选择当前单元格,然后改变它的辅助视图。你的视图对象会去newView所在的位置。

+0

没有相当的工作。查看更新后的问题 – cannyboy 2010-05-20 16:39:55

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。

+0

willSelectRowAtIndexPath返回NSIndexPath *,不是void。 – oltman 2012-03-01 04:49:24

+0

你说得对。这是我对'willSelectRowAtIndexPath'(编辑)的误会。但'didSelectRowAtIndexPath'返回void。 **你是否因为('willSelectRowAtIndexPath')的这个错误建议而导致-1或**在实现示例中也存在错误**? – 2012-03-01 12:27:32

0

使用的UIImageView的highlightedImage属性:

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