【问题标题】:Allow UITableView to reorder, but not delete in edit mode, and enable swipe to delete anyway允许 UITableView 重新排序,但在编辑模式下不删除,并启用滑动删除
【发布时间】:2016-01-23 17:22:05
【问题描述】:

我有一个 UITableView (iOS 9) 我已经通过滑动实现了两个动作(一个是删除) 我有一个编辑按钮来启用编辑模式(重新排序行)

为此,我实现了

    override func setEditing(editing: Bool, animated: Bool) {

    super.setEditing(editing, animated:animated)

    if (!isInSwipeDeleteMode) {
        if (self.tableView.editing) {
            metAJourPersonnes()
            tableView.reloadData()
        }
        else {
            tableView.reloadData()
        }
    }
}

    override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {

    if (indexPath.row < personnes.count) {
        return true
    } else {
        return false
    }
}

override func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
    let pers = personnes[sourceIndexPath.row]
    personnes.removeAtIndex(sourceIndexPath.row)
    if (destinationIndexPath.row < personnes.count)
    {
        personnes.insert(pers, atIndex: destinationIndexPath.row)
    } else {
        personnes.append(pers)
    }
    tableView.reloadData()
}

override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
    if (indexPath.row < personnes.count) {
        return .Delete
    } else {
        return .None
    }
}

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
    let deleteClosure = { (action: UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in
        print("Delete closure called")
        self.tableView(tableView, commitEditingStyle: .Delete, forRowAtIndexPath: indexPath)
    }

    let modifyClosure = { (action: UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in
        print("More closure called")            
        self.performSegueWithIdentifier("modifPersonne", sender: indexPath)
    }

    let deleteAction = UITableViewRowAction(style: .Default, title: "Supprimer", handler: deleteClosure)
    let modifyAction = UITableViewRowAction(style: .Normal, title: "Modifier", handler: modifyClosure)   
    return [deleteAction, modifyAction]

}

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    switch editingStyle {

    case .Delete:
        // Delete in the coreData base         
        personnes.removeAtIndex(indexPath.row)
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)

    default:    break
    }
}

一切正常,但我希望编辑模式仅用于重新排序。我不希望出现红色减号,但我想保留滑动操作。

这可能吗?似乎在编辑模式下禁用删除确实会禁用滑动删除手势。

【问题讨论】:

  • 用您的相关表格视图方法更新您的问题,以便我们可以看到您到目前为止所拥有的内容。把编辑、删除、移动相关的方法全部贴出来就行了。
  • 您需要展示这些方法的实际实现。
  • 我不认为发布实际的实现会有帮助。它只会使问题更长。在为这种方法编写标准代码时,它给出了我的结果(标准),即在滑动中删除和其他操作以删除,并在编辑模式下再次使用减号样式。如果这是可能的,那是我应该添加的东西,而不是糟糕的实现,恕我直言
  • 发布您的代码非常有帮助。它使我们能够看到您实际拥有的内容,并让您更轻松地告诉您需要更改的内容。

标签: ios uitableview


【解决方案1】:

我相信您唯一需要更改代码的就是editingStyleForRowAtIndexPath 函数。如果表格视图未处于编辑模式,则仅返回 .Delete

这种方式滑动删除仍然有效(不在编辑模式下),并且当您切换到编辑模式时,无法删除该行。

【讨论】:

  • 谢谢@maddy,我没有想到这一点。更改为 ` override fun tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle { if (indexPath.row
  • 很高兴我能帮上忙。不要忘记接受最能解决您问题的答案。
猜你喜欢
  • 2010-11-01
  • 2013-05-25
  • 1970-01-01
  • 1970-01-01
  • 2012-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多