UITableViewCell定制配件 - 获取一排配件

Emi*_*mil 14 iphone row accessory uitableview ios4

我有一个很大的问题.我正在尝试在每个UITableViewCell中创建一个最喜欢的按钮UITableView.这非常有效,我现在按下时会执行一个动作和选择器.

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;
Run Code Online (Sandbox Code Playgroud)

和选择器:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

现在,问题在于:我想知道按下的配件属于哪一行.我怎样才能做到这一点?

谢谢 :)

小智 20

我会使用以下代码:

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


Gia*_*iao 16

稍微改变你的行动.

- (void)action:(id)sender forEvent:(UIEvent *)event

在那个方法里面:

NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:[[[event touchesForView:sender] anyObject] locationInView:tableView]];

这应该会得到行的索引路径.