gpt4 book ai didi

ios - 如何使用 UIActionSheet 确认删除 UITableViewCell

转载 作者:行者123 更新时间:2023-11-28 17:38:16 26 4
gpt4 key购买 nike

我有一个标准的 CoreData/NSFetchedResultsController/UITableView 设置。顶部没有编辑按钮,但右击一行会显示删除按钮。如果按下删除按钮,我想弹出一个 UIActionSheet 来确认删除。

在 UIActionSheet 之前,我刚刚实现了 tableView:commitEditingStyle:forRowAtIndexPath:,在其中,我为 indexPath 在 managedObjectContext 上调用了 deleteObject: 并让 FetchedResultsController 覆盖处理它的视觉方面。

现在,我有 tableView:commitEditingStyoe:forRowAtIndexPath: 创建 UIActionSheet 并显示它。我实现了 actionSheet:clickedButtonAtIndex: 并在按下删除按钮的情况下放置了 deleteObject:。问题是我需要删除项目的 indexPath,但它不再在范围内。

我...

A) 删除 tableView:commitEditingStyle:forRowAtIndexPath: 中的 ManagedObject 并在 actionSheet:clickedButtonAtIndex: 中保存或回滚 ?

B) 将索引路径存储为属性,以便我可以在 tableView:commitEditingStyle:forRowAtIndexPath: 中设置它并在 actionSheet:clickedButtonAtIndex: 中读取它?

C) 其他...

选项 A 似乎比必要的开销更多。无论哪种方式,它都会被删除,只是有时会回滚。

选项 B 似乎非常鹰派。像这样的外部值似乎不太适合面向对象的模型。

在 UIActionsSheet 中是否有一个通用对象来传递这样的值?还是我还缺少其他东西?

最佳答案

我需要一个类似的解决方案。我通过创建一个 ivar NSIndexPath * currentPath; 解决了这个问题,并将以下想法用于 tableView:commitEditingStyle:forRowAtIndexPath:

- (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
UIAlertView * alert;
UITableViewCell * cell;
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// save current indexPath
[currentPath release];
currentPath = [indexPath retain];

// display warning
alert = [[UIAlertView alloc] initWithTitle:@"Really?"
message:@"Are you really really sure you want to delete this item?"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Delete", nil];
[alert show];
[alert release];

// clear delete confirmation from table view cell
cell = [tableView cellForRowAtIndexPath:indexPath];
[cell setEditing:NO animated:NO];
[cell setEditing:YES animated:YES];
};
return;
}

如果用户点击删除按钮,我会从 UITableViewCell 中删除行并从数据源中删除条目。如果他们点击取消,我将忽略该操作。

关于ios - 如何使用 UIActionSheet 确认删除 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9124446/

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