如何设置UITableViewCell高亮显示的图像

时间:2012-09-28 14:44:53

标签: iphone objective-c ios xcode uitableview

每当用户在cell中突出显示UITableView(未选中)时,我想启动一种方法。你能告诉我,怎么可能这样做?

我想这样做是因为我有一个带有图片的自定义单元格,我希望每当用户突出显示单元格时都会更改图片。

UPD:通过突出显示我的意思是用户只是突出显示一个单元格,而不是从中释放手指。通过选择我的意思是什么时候启动didSelectRowAtIndexPath(因此用户在按下它后从手指中释放手指)

3 个答案:

答案 0 :(得分:2)

您如何设想用户“突出显示”一个单元而不是“选择”单元?

在iOS(或任何基于触摸的环境中),没有概念只是突出显示单元格而不是选择单元格。用户触摸单元格时获得的唯一回调是didSelectRowAtIndexPath:

这里可能值得阅读documentation on tables

<强>更新

啊好的,在这种情况下你想设置细胞imageView的highlightImage属性有点像这样;

cell.imageView.image = [UIImage imageNamed:@"normal_image.png"];
cell.imageView.highlightedImage = [UIImage imageNamed:@"highlighted_image.png"];

答案 1 :(得分:0)

我不明白你的问题..你不想使用方法selectRowAtIndexPath?

如果要在用户选择行时执行方法:

- 您可以使用方法selectRowAtIndexPath并执行您的方法。

您还可以在单​​元格内部创建一个不可见的UIButton,单击一个单元格时,您将单击按钮并执行您的方法。 。

答案 2 :(得分:0)

从iOS 6.0开始, UITableViewDelegate 有3种处理单元格突出显示的方法:

- tableView:shouldHighlightRowAtIndexPath:
- tableView:didHighlightRowAtIndexPath:
- tableView:didUnhighlightRowAtIndexPath:

你应该使用它们,就像在这个例子中一样:

- (BOOL)tableView:(UITableView*)tableView shouldHighlightRowAtIndexPath:(NSIndexPath*)indexPath
{
    return YES;
}

- (void)tableView:(UITableView*)tableView didHighlightRowAtIndexPath:(NSIndexPath*)indexPath
{
    MyTableViewCell* cell = (MyTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
    cell.myImageView.image = [UIImage imageNamed:@"myCellHigh"];    
}

- (void)tableView:(UITableView*)tableView didUnhighlightRowAtIndexPath:(NSIndexPath*)indexPath
{
    MyTableViewCell* cell = (MyTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
    cell.myImageView.image = [UIImage imageNamed:@"myCellUnhigh"];  
}
相关问题