复制所选单元格中的数据(快速)

B2F*_*2Fq 3 xcode copy uitableview ios swift

我在UITableViewCell中向左/向右滑动?

我该如何做才能复制所选单元格中的数据?

我尝试使用它:

let cell = self.table.cellForRow(at: indexPath)
UIPasteboard.general.string = cell?.detailTextLabel?.text
Run Code Online (Sandbox Code Playgroud)

但是应用程序崩溃

在此处输入图片说明

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell")!
    cell.textLabel?.text = myData[indexPath.row]
    return cell
}

func tableView(_ tableView: UITableView,leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?{
    let closeAction = UIContextualAction(style: .normal, title:  "Close", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
        print("Copy")

            //let cell = self.table.cellForRow(at: indexPath)
            //UIPasteboard.general.string = cell?.detailTextLabel?.text

        success(true)
    })
    closeAction.title = "Copy"
    closeAction.backgroundColor = .purple

    return UISwipeActionsConfiguration(actions: [closeAction])
}
Run Code Online (Sandbox Code Playgroud)

Dha*_*esh 5

更换:

let cell = self.table.cellForRow(at: indexPath)
UIPasteboard.general.string = cell?.detailTextLabel?.text
Run Code Online (Sandbox Code Playgroud)

带有:

let cell = tableView.cellForRow(at: indexPath)
UIPasteboard.general.string = cell?.textLabel?.text
Run Code Online (Sandbox Code Playgroud)

因为您正在使用tableViewin cellForRowAt方法。所以我tabletableView和在第二行替换detailTextLabeltextLabel

这应该工作。