【问题标题】:How to use a UIActionSheet to confirm deletion of a UITableViewCell如何使用 UIActionSheet 确认删除 UITableViewCell
【发布时间】:2012-02-03 06:14:16
【问题描述】:

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

【问题讨论】:

    标签: ios uitableview cocoa-touch nsfetchedresultscontroller uiactionsheet


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-11
        相关资源
        最近更新 更多