使用swift 4在tableview中选择行时显示两个按钮

时间:2018-02-13 15:54:11

标签: swift uitableview swift4

当用户选择单元格时,是否有办法显示UITableViewRowAction按钮(与UITableViewRowAction默认工作方式相反,这需要tableview处于编辑模式)。

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]?
    {
        let shareAction  = UITableViewRowAction(style: .Normal, title: "Share") { (rowAction, indexPath) in
            print("Share Button tapped. Row item value = \(self.itemsToLoad[indexPath.row])")

            self.displayShareSheet(indexPath)
        }
        let deleteAction  = UITableViewRowAction(style: .Default, title: "Delete") { (rowAction, indexPath) in
            print("Delete Button tapped. Row item value = \(self.itemsToLoad[indexPath.row])")
        }
        shareAction.backgroundColor = UIColor.greenColor()
        return [shareAction,deleteAction]
    }

1 个答案:

答案 0 :(得分:1)

您不太可能在另一个答案中找到代码,您可以将其放入项目中并使其正常工作,尤其是如果该代码是用其他语言编写的话。您将修改您的代码以执行您想要的操作。

通常,您不应直接操纵细胞。您应该设置数据模型以包含用于配置单元的状态信息。您可能有一个结构数组,其中每个数组条目描述要在单个单元格中显示的信息。 (如果使用分段表视图,则可以使用数组数组,其中外部数组是您的部分,每个内部数组包含部分中行的结构。)

下面概述了如何实现这一目标:

struct CellData {
   var title: String 
   var showButtons: Bool
}

然后在您的tabView(_:didSelect:)方法中,将先前选择的indexPath(如果有)上的showButtons标志设置为false,将新选择的单元格上的showButtons标志设置为{{1 },并调用true重新绘制取消选择的单元格和新选择的单元格。

如果您允许一次选择多个单元格,则不会清除先前所选单元格的reloadRows(at:with:)标记。

showButtons方法中,使用showButtons标志来决定是否应隐藏按钮。