【问题标题】:UITableView editActionsForRowAt not reacting properly to nil in Swift 5UITableView editActionsForRowAt 在 Swift 5 中对 nil 没有正确反应
【发布时间】:2019-08-17 12:25:47
【问题描述】:

我有一个 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
        }
    }

【问题讨论】:

    标签: uitableview swift5


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • Matt,我重构为使用 trailinSwipeActionsConfigurationForRowAt 并且得到了相同的结果。 (我已经用重构的代码编辑了原始问题)
      • 我解决了,我必须添加助手“canEditRowAt”并提供“扩展”逻辑。原始问题中提供的答案。感谢您的指点。
      • 不要在你的问题中给出答案。将其作为答案并接受您自己的答案!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-14
      • 2011-02-19
      • 2021-07-22
      • 1970-01-01
      • 2015-09-17
      相关资源
      最近更新 更多