ios 按下编辑操作时获取 table 查看单元格索引

ios Get table view cell index when edit action is pressed

我有一个 table 视图,其中包含具有编辑操作的单元格。当用户向左滑动其中一个单元格时,您可以看到编辑操作按钮。

当用户点击其中一个按钮时,我需要获取点击这些按钮的单元格的索引路径。我该怎么做?

注意,编辑操作按钮不是 UIButton,它们是这样制作的。

-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
     UITableViewRowAction *button = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Reply" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
                                    {

 nil;

                                    }];

    button.backgroundColor = [UIColor colorWithRed:0.1 green:0.4 blue:0.9 

    alpha:1.0]; //arbitrary color
    }

试试这个

把这个写在你的 UITableViewCell

NSIndexPath *indexPath = [(UITableView *)self.superview indexPathForCell: self];

希望对您有所帮助。

您需要重写委托方法,当您为 edit/delete 选项滑动行时将获取该方法。

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {

        NSLog(@"canEditRowAtIndexPath get called");
        NSLog(@"Index is %ld",indexPath.row);
        return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //delete object here
        NSLog(@"Index is %ld",indexPath.row);
        [tableView reloadData]; // tell table to refresh now
   }
}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"accessoryButtonTappedForRowWithIndexPath method get called");
    NSLog(@"Index is %ld",indexPath.row);
}

第一组

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{......
editbutton.tag=index.row
   [editbutton addTarget:self action:@selector(clickOn:) forControlEvents:UIControlEventTouchUpInside];

....
}

比 在

-(IBAction)clickOn:(id)sender
{
    UIButton *btn=(UIButton *)sender;
    nslog(@"%d",btn.tag);


}