UITableView editActionsForRowAt 对 Swift 中的 nil 没有正确反应 5

UITableView editActionsForRowAt not reacting properly to nil in Swift 5

我有一个 UITableView 适用于 Swift 的多个版本,但在 Swift 5 中,它开始在向左滑动时显示 'delete' 动作。

其中一行是 'swipe-able'('Expanded' 行)而其他行不是,所以我 return 一个 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' 相同的行为。

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

现在我只是 return 显示与狗相关的表情符号 () 的虚拟操作。

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
        }
    }
如果目标是控制滑动时发生的情况,

editActionsForRowAt 已经过时了。 UITableViewRowAction 也是如此。

您应该使用 tableView(_:trailingSwipeActionsConfigurationForRowAt:)

回答 我必须添加 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"
    }