将UIImage添加到UITableViewCell以获取“State Touched”

时间:2010-09-02 14:58:56

标签: iphone objective-c uitableview

UITableViewCell

中为触控状态添加图像的最佳方法是什么?

我知道如何设置cell.selectionStyle,但实际上我需要在用户触摸它时添加一个图像(每行不同)(并且它将新视图推入)像“按钮”国家感动“。

实现这一目标的最佳方法是什么?我试过

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { }

或者我必须在

中进行
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

谢谢!

2 个答案:

答案 0 :(得分:1)

我建议您创建自定义UITableViewCell并使用touchesBegantouchesEnded

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchedBegan:touched withEvent:event];
    // ... display image 2, hide image 1
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchedEnded:touched withEvent:event];
    // ... display image 1, hide image 2
}

答案 1 :(得分:1)

我认为你需要两者:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

对于didSelectRow,您需要存储当前选定的单元格索引。然后你调用[tableView reloadData]

然后在cellForRowAtIndexPath:中,您需要设置cell.imageView.image = [UIImage imageNamed:@"your_image_here"];