gpt4 book ai didi

iphone - 获取所选单元格的索引路径

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:03:22 24 4
gpt4 key购买 nike

当用户在 UITableView 编辑模式下点击默认的 delete 按钮时,用户应该得到一个 alertview,如果他在 AlertView 中再次点击 Delete,该行应该被删除.最初,我在没有 AlertView 的情况下使用以下代码完成了此操作,并且运行良好。

  [[self categoriesArray]removeObjectAtIndex:[indexPath row]];
NSArray *indexPathsToRemove = [NSArray arrayWithObject:indexPath];
[self.tableView deleteRowsAtIndexPaths:indexPathsToRemove withRowAnimation:UITableViewRowAnimationLeft];
[self.categoriesArray writeToFile:[self dataFilePath] atomically:YES];

但是现在,因为我必须在 alertview 委托(delegate)方法中使用相同的代码。我不知道如何获取 [indexPath row]

最佳答案

将您的索引路径设置为 UIAlertView 标记并从 Delegate 获取。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath // indexPath is here
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Are you sure want to delete" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Delete", nil];
[alertView setTag:indexPath.row]; // Assigning here.
[alertView show];
}
}
// UIAlertView Delegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"%d",alertView.tag); // Your Indexpath is here

**Edited:**

NSIndexPath * path = [NSIndexPath indexPathForRow:alertView.tag inSection:0];
[[self categoriesArray]removeObjectAtIndex:[path row]];
NSArray * indexPathsToRemove = [NSArray arrayWithObject:path];
[self.tableView deleteRowsAtIndexPaths:indexPathsToRemove withRowAnimation:UITableViewRowAnimationLeft];
[self.categoriesArray writeToFile:[self dataFilePath] atomically:YES];
}

关于iphone - 获取所选单元格的索引路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15834317/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com