表格视图、单元格编辑和拖动

Tableviews, cell editing and dragging

我有一个 swift 应用程序,它有一个表格视图,其中的单元格是动态的。我想要实现的是,用户可以侧滑一个单元格,它会显示两个图块,一个用于编辑,一个用于删除(这方面的一个例子是在消息应用程序中,您可以在其中侧滑删除选项)

我有两个要显示的图块:

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
    let editRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: " Edit ", handler:{action, indexpath in
    });
    moreRowAction.backgroundColor = UIColor.orangeColor()

    let deleteRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete", handler:{action, indexpath in

    return [editRowAction, deleteRowAction]
}

我遇到的第一个问题是,当编辑选项被 select 编辑时,如何以编程方式关闭侧滑,以便隐藏图块,用户可以看到单元格中的文本字段?

第二个问题,当单元格处于编辑模式时,我希望能够在表格视图中移动单元格:按照教程我已经实现了以下内容:

func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true
}
func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
    let sourceRow = sourceIndexPath.row;
    let destRow = destinationIndexPath.row;
    let object = ArrayList.objectAtIndex(sourceRow)
    ArrayList.removeObjectAtIndex(sourceRow)
    ArrayList.insertObject(object, atIndex: destRow)
}

并在我想将单元格置于移动模式时设置以下内容

    TableView.setEditing(true, animated: true)

哪种方式可以满足我的需求,但是当我将其置于编辑模式时,我会在单元格左侧看到删除图标(带有白色破折号的红色圆圈),这是我不想要的,理想情况下我'我喜欢我自己的图标,这样用户可以 select 并四处拖动单元格,但我觉得这可能会稍微推动它。

谢谢

要删除删除按钮,您应该能够将编辑样式设置为 none(或其他)

func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
    return tableView.editing ? UITableViewCellEditingStyle.None : UITableViewCellEditingStyle.Delete
}

要在滑动后隐藏到按钮,您可以设置 tableView.editing 或重新加载单元格。

tableView.editing = false

备选

tableView.reloadRowsAtIndexPaths(indexPaths: [NSIndexPath], withRowAnimation: UITableViewRowAnimation.Automatic)

//所有涉及的委托方法:

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true
}

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
    let editRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: " Edit ", handler:{action, indexpath in
            self.tableView.editing = false
    })

    let deleteRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete", handler:{action, indexpath in
            self.tableView.editing = false
    })

    return [editRowAction, deleteRowAction]
}

func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
    return tableView.editing ? UITableViewCellEditingStyle.None : UITableViewCellEditingStyle.Delete
}

func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true
}

func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {

}