3

我有一个 UITableView 为多个版本的 Swift 工作,但是在 Swift 5 中,它开始在左滑动时呈现“删除”操作。

其中一行是“可滑动”(“扩展”行),而其他行不是,所以我返回一个 nil 来代替 RowAction。

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    if listOfCurrentAwards[indexPath.row] != "Expanded" {
        print(">>> Skipping row \(indexPath.row)")
        return nil
    } else {
        let share = UITableViewRowAction(style: .normal, title: "Share") { action, index in
            self.prepareOneDogMessageShareSheet(row: indexPath.row)
        }
        share.backgroundColor = UIColor.blue
        return [share]
    }
}

我也得到了与旧的“editActionsForRowAtIndexPath”相同的行为。

其他人看到同样的事情吗?你找到工作了吗?

现在我只是返回一个显示与狗相关的表情符号()的虚拟动作。

if listOfCurrentAwards[indexPath.row] != "Expanded" {
   //TBD - remove the following line if the nill action is supported/fixed.
   let dogEmoji = ["","","","","",""]
   let share = UITableViewRowAction(style: .normal, title: dogEmoji.randomElement()) { action, index in
   print(">>> Skipping row \(indexPath.row)")
   }
   return [share] //nil
} else ...

更新 1 即使重构使用 trailingSwipeActionsConfigurationForRowAt 也不起作用,我得到了相同的结果。

    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
    {
        if listOfCurrentAwards[indexPath.row] != "Expanded" {
            print(">>> Swiping not enabled for row \(indexPath.row)")
            return nil
        } else {
            print(">>> 'Share' swipped on \(indexPath.row)")
            let shareAction = UIContextualAction(style: .normal, title: "Share") { (action, view, handler) in
                print(">>> 'Share' clicked on \(indexPath.row)")
                self.prepareOneDogMessageShareSheet(row: indexPath.row)
            }
            shareAction.backgroundColor = UIColor.blue
            let configuration = UISwipeActionsConfiguration(actions: [shareAction])
            configuration.performsFirstActionWithFullSwipe = true //false to not support full swipe
            return configuration
        }
    }
4

2 回答 2

3

答案 我必须添加 canEditRowAt 帮助程序,允许我从 trailingSwipeActionsConfigurationForRowAt 中移动一些逻辑。

    func tableView(_ tableView: UITableViewbcgdfgdfg, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?vdfgh
    {
        print(">>> Trying to swipe row \(indexPath.row)")
        let shareAction = UIContextualAction(style: .normal, title: "Share") { (action, view, handler) in
            print(">>> 'Share' clicked on \(indexPath.row)")
            self.prepareOneDogMessageShareSheet(row: indexPath.row)
        }
        shareAction.backgroundColor = UIColor.blue
        let configuration = UISwipeActionsConfiguration(actions: [shareAction])
        return configuration
    }

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return listOfCurrentAwards[indexPath.row] == "Expanded"
    }
于 2019-03-27T19:10:07.210 回答
2

editActionsForRowAt如果目标是控制滑动时发生的事情,则已过时。UITableViewRowAction 也是如此。

你应该使用tableView(_:trailingSwipeActionsConfigurationForRowAt:).

于 2019-03-27T02:05:19.543 回答