如何使用 UIActionSheet 确认删除 UITableViewCell

发布于 2025-01-02 01:20:46 字数 1035 浏览 1 评论 0原文

我有一个标准的 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 中是否有一个通用对象用于传递这样的值?还是我还缺少其他东西?

I have a standard CoreData/NSFetchedResultsController/UITableView setup. There is no edit button on top, but right-swiping a row will reveal the delete button. If the delete button pressed, I want to pop up a UIActionSheet to confirm the deletion.

Before the UIActionSheet, I just implemented tableView:commitEditingStyle:forRowAtIndexPath: and in it, I called deleteObject: on the managedObjectContext for the indexPath and let the FetchedResultsController overrides handle the visual aspect of it.

Now, I have tableView:commitEditingStyoe:forRowAtIndexPath: create the UIActionSheet and show it. I implemented actionSheet:clickedButtonAtIndex: and put the deleteObject: in the case where the delete button is pressed. The problem is that I need the indexPath of the item to delete and it is no longer in scope.

Do I...

A) delete the ManagedObject in tableView:commitEditingStyle:forRowAtIndexPath: and either save or roll back in actionSheet:clickedButtonAtIndex: ?

B) store the index path as a property so I can set it in tableView:commitEditingStyle:forRowAtIndexPath: and read it in actionSheet:clickedButtonAtIndex:?

C) something else...

Option A seems like more overhead than necessary. Either way it will be deleted, just sometimes it will be rolled back.

Option B seems very hawkish. An external value like that doesn't seem to fit the object oriented model very well.

Is there a generic object in UIActionsSheet for passing in values like this? Or is there something else I'm missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

澉约 2025-01-09 01:20:46

我需要一个类似的解决方案。我通过创建一个 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 并从数据源中删除该条目。如果他们点击取消,我就会忽略该操作。

I needed a similar solution. I solved this by creating an ivar NSIndexPath * currentPath; and used the following idea for 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;
  }

If the user taps the delete button, I then remove the row from the UITableViewCell and remove the entry from the data source. If they tap cancel, I simply ignore the action.

橘味果▽酱 2025-01-09 01:20:46

我会将托管对象本身存储在 -tableView:commitEditingStyle:forRowAtIndexPath: 方法中的属性中,而不是 indexPath:

self.deleteCandidate = [self.controller objectAtIndexPath:indexPath]

然后在 -actionSheet:clickedButtonAtIndex: 中删除(或不删除)该对象:,并在任一情况下设置self.deleteCandidate = nil

原因是获取的结果控制器对象可能在调用的这两个方法之间发生变化。

另一种选择是使用“关联引用”(objc_setAssociatedObject、objc_getAssociatedObject)来存储从 UIActionSheet 到相关对象的引用。

Instead of the indexPath I would store the managed object itself in a property in the -tableView:commitEditingStyle:forRowAtIndexPath: method:

self.deleteCandidate = [self.controller objectAtIndexPath:indexPath]

and then delete that object (or not) in -actionSheet:clickedButtonAtIndex:, and set self.deleteCandidate = nil in either case.

The reason is that the fetched results controller objects might change between those two methods being called.

Another option is to use "Associative References" (objc_setAssociatedObject, objc_getAssociatedObject) to store a reference from the UIActionSheet to the object in question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文