【问题标题】:Tableviews, cell editing and dragging表格视图、单元格编辑和拖动
【发布时间】:2015-10-23 22:29:55
【问题描述】:

我有一个带有表格视图的 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]
}

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

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

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)

哪种是我想要的,但是当我将其置于编辑模式时,我会在单元格左侧看到我不想要的删除图标(带有白色短划线的红色圆圈),理想情况下我想要我自己的图标,因此用户可以选择并拖动单元格,但我觉得这可能会稍微推动它。

谢谢

【问题讨论】:

    标签: swift uitableview tableviewcell


    【解决方案1】:

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

    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) {
    
    }
    

    【讨论】:

    • 您好,感谢您的回复,如果我设置 func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle { return UITableViewCellEditingStyle.None } 它会停止完全显示瓷砖
    • 你能根据你正在执行的操作来打开和关闭它吗?
    • 当我希望单元格图块可见时,我必须将其切换到什么位置?我尝试过的似乎不允许瓷砖可见?
    • 我更新了答案。我试过这个版本,它对我有用
    猜你喜欢
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多