UITableView editActionsForRowНе правильно реагирует на ноль в Swift 5 - PullRequest
0 голосов
/ 27 марта 2019

У меня был UITableView, работающий для нескольких версий Swift, но с Swift 5 он начал представлять действие «удалить» при левом пролистывании.

Одна из строк «смахивает» («расширенная»), а другие - нет, поэтому я возвращаю ноль вместо 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
        }
    }

Ответы [ 2 ]

1 голос
/ 27 марта 2019

Ответ Мне пришлось добавить помощник canEditRowAt, позволяющий переместить часть логики из trailingSwipeActionsConfigurationForRowAt. * ​​1005 *

    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
    {
        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"
    }
1 голос
/ 27 марта 2019

editActionsForRowAt устарел, если целью является контроль того, что происходит, когда вы проводите.Так же как и UITableViewRowAction.

Вы должны использовать tableView(_:trailingSwipeActionsConfigurationForRowAt:).

...