ios - 如何将 tableView 的多行传输到另一个 ViewController

标签 ios swift segue

我正在尝试在我的应用程序中添加一项功能,以便一次将多个成员添加到一个操作中。成员列在 tableView 中,用户可以使用 .allowsMultipleSelection = true 函数一次选择多行。我得到了以下代码,但这不起作用。我认为我的想法会奏效,但不会像我在代码中那样:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

       guard let destination = segue.destination as? AddMultipleMemberTransactionViewController,
        let selectedRows = multipleMemberTableView.indexPathsForSelectedRows else  {
            return
        }
        destination.members = members[selectedRows]
    }

这里有人知道我该如何解决这个问题,因为有一个错误:

Cannot subscript a value of type '[Member?]' with an index of type '[IndexPath]'

我在应用程序中拥有相同的功能,但仅适用于一名成员(member)。我在 indexPathForSelectedRow 之后的 let selectedRows 行中有一个 .row。 indexPathsForSelectedRows 是否有类似的功能?
或者这是错误的方法吗?

最佳答案

你需要

destination.members = selectedRows.map{ members[$0.row] }

关于ios - 如何将 tableView 的多行传输到另一个 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51730140/

相关文章:

ios - 我应该如何以及为什么应该在 Xcode 中使用 'In-App Purchase Content' 模板?

ios - 没有 moreViewController 的 UITabBarController 的最大选项卡数

swift - 一元运算符 '++'

ios - 推送 View Controller 时无缝动画 "from left to right"?

ios - 从 UIImage 输出固定大小的 JPEG

ios - 当弹出窗口消失时,保持 UITextView 中的文本突出显示

ios - 如何在 Swift 5 中的 UIScrollView 内以编程方式生成的 UIView 上实现点击手势

swift - 快速搜索一个字符串中的多个单词

ios - 推送 View Controller 无法设置动画

ios5 - 使用原型(prototype)单元执行 segue