如何在单击自定义附件按钮时获取UITableView中单元格的索引路径?

Dev*_*per 27 iphone xcode objective-c

我有UITableView.In,我已经通过点击附件按钮创建与附件button.Now自定义单元格我想创建与编辑细胞functionality.For另一个观点,即如何找到该单元格的索引路径?以及如何传递那个细胞的价值?

我如何调用以下方法:

 - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)

Emp*_*ack 84

首先要注意的是,当您在单元格中使用自定义accessoryView时,将不会调用tableView:accessoryButtonTappedForRowWithIndexPath: delegate方法.您必须向要添加的按钮添加一些目标/操作作为自定义附件并自行处理点击操作.您应该添加这样的自定义配件,

UIButton *accessory = ...;
[accessory addTarget:self action:@selector(onCustomAccessoryTapped:) forControlEvents:UIControlEventTouchUpInside];
...
cell.accessoryView = accessory;
Run Code Online (Sandbox Code Playgroud)

onCustomAccessoryTapped:方法中,你必须得到像这样的索引路径,

- (void)onCustomAccessoryTapped:(UIButton *)sender {

    UITableViewCell *cell = (UITableViewCell *)sender.superview;
    NSIndexPath *indexPath = [tableView indexPathForCell:cell];

    // Now you can do the following
    [self tableView:tableView accessoryButtonTappedForRowWithIndexPath:indexPath];

    // Or you can do something else here to handle the action
}
Run Code Online (Sandbox Code Playgroud)