UITableViewCell 自定义附件 - 获取附件的行 [英] UITableViewCell Custom accessory - get the row of accessory

查看:19
本文介绍了UITableViewCell 自定义附件 - 获取附件的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的问题.我正在尝试在 UITableView 中的每个 UITableViewCell 上创建一个收藏按钮.这很好用,我目前在按下时执行了一个动作和选择器.

I have a pretty big issue. I am trying to create a favorite-button on every UITableViewCell in a UITableView. That works very good, and I currently have an action and selector performed when pressed.

accessory = [UIButton buttonWithType:UIButtonTypeCustom];
[accessory setImage:[UIImage imageNamed:@"star.png"] forState:UIControlStateNormal];
accessory.frame = CGRectMake(0, 0, 15, 15);
accessory.userInteractionEnabled = YES;
[accessory addTarget:self action:@selector(didTapStar) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = accessory;

和选择器:

- (void) didTapStar {
    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:/* indexPath? */];

    accessory = [UIButton buttonWithType:UIButtonTypeCustom];
    [accessory setImage:[UIImage imageNamed:@"stared.png"] forState:UIControlStateNormal];
    accessory.frame = CGRectMake(0, 0, 26, 26);
    accessory.userInteractionEnabled = YES;
    [accessory addTarget:self action:@selector(didTapStar) forControlEvents:UIControlEventTouchDown];
    newCell.accessoryView = accessory;
}

现在,问题来了:我想知道按下的附件属于哪一行.我该怎么做?

Now, here's the problem: I want to know what row the accessory that was pressed belongs to. How can I do this?

谢谢你:)

推荐答案

我会使用以下代码:

- (void)didTapStar:(id)sender {
  NSIndexPath *indexPath = [tableView indexPathForCell:(UITableViewCell *)[sender superview]];
}

这篇关于UITableViewCell 自定义附件 - 获取附件的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆