【问题标题】:pass the data from uitableview to other uiview将数据从 uitableview 传递到其他 uiview
【发布时间】:2017-02-20 15:18:00
【问题描述】:

我是 swift 和 IOS 的新手,将字典数据传递给其他 uiview 时遇到问题,谁能帮我解决?

LessonsTableViewController:

var mylessons = [
    ["title":"Posture", "subtitle":"Set up your body", "bgimage":"1", "lesimage":"l1"],
    ["title":"Breathing", "subtitle":"Breathing deeply", "bgimage":"2", "lesimage":"l2"],
    ["title":"Breathing", "subtitle":"Breathing Exercise", "bgimage":"3", "lesimage":"l3"],
    ["title":"Health", "subtitle":"Do’s & Don’ts", "bgimage":"4", "lesimage":"l4"]
]

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! LessonsTableViewCell

    let lessonsObject = mylessons[indexPath.row]

    cell.backgroundImageView.image = UIImage(named: lessonsObject["bgimage"]!)
    cell.titleLabel.text = lessonsObject["title"]
    cell.subtitleLabal.text = lessonsObject["subtitle"]

    return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "LessonSegue", sender: mylessons[indexPath.row])
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?){
    let lessegue = segue.destination as! LessonDetailsViewController
    lessegue.SelectedLessons = mylessons
}

LessonDetailsViewController:

    @IBOutlet weak var LTitle: UILabel!

var SelectedLessons = [Dictionary<String, String>()]

override func viewDidLoad() {
    super.viewDidLoad()
    LTitle.text = SelectedLessons["title"]
    // Do any additional setup after loading the view.
}

最后,它有一个错误“Cannot subscript a value of type '[Dictionary]' with an index of 'String'.

【问题讨论】:

    标签: ios swift uitableview segue


    【解决方案1】:

    首先您的 SelectedLessons 类型错误。你需要使用类似 tis 的东西

    var SelectedLessons:Dictionary<String, String>?
    

    你需要过去正确的对象。

    override func prepare(for segue: UIStoryboardSegue, sender: Any?){
        let lessegue = segue.destination as! LessonDetailsViewController
        lessegue.SelectedLessons = sender as? Dictionary<String,String>
    }
    

    【讨论】:

    • 非常感谢,它现在对我的应用程序有效。另外, LTitle.text = SelectedLessons["title"] 需要更改 LTitle.text = SelectedLessons?["title"]
    【解决方案2】:

    你应该声明

    var SelectedLessons = [String, String]()
    

    你当前的声明是一个字典数组

    【讨论】:

      【解决方案3】:

      你有很多问题。

      首先是编码风格问题。变量名应以小写字母开头,因此SelectedLessons 应为selectedLessons

      其次,您可能希望将用户选择的课程传递到目的地,而不是整个数组。

      您的数组mylessons 是一个字典数组:(键入[[String:String]]

      您应该将变量命名为 LessonDetailsViewController selectedLesson(单数,以小写字母开头)并输入 [String: String](单节课)。

      那么您的 prepareForSegue 可能如下所示:

      override func prepare(for segue: UIStoryboardSegue, sender: Any?){
          guard  
            let lessegue = segue.destination as? LessonDetailsViewController,
            let selectedRow = tableView.indexPathForSelectedRow?.row else {
              print("Error. No row selected. Exiting."
              fatalError()
            }
          lessegue.selectedLesson = myLessons[selectedRow]
      }
      

      (对于没有选定行的情况,上面的代码应该有更好的错误处理,但它应该给你这个想法。)

      编辑:

      顺便说一句,编写prepare(for:) 方法并不是一个好主意,就好像你只会使用单一类型的单一视图控制器一样。返回并扩展应用程序以添加额外的 segue 是很常见的,如果这样做,上面的代码将崩溃。最好使用 switch 语句:

      override func prepare(for segue: UIStoryboardSegue, sender: Any?){
          switch segue.destination {
            case let lessegue as LessonDetailsViewController:
              guard  
              let selectedRow = tableView.indexPathForSelectedRow?.row else {
                print("Error. No row selected. Exiting."
                fatalError()
              }
              lessegue.selectedLesson = myLessons[selectedRow]
            default:
              print("Unrecognized segue. Exiting."
              fatalError()
          }
      }
      

      该语法创建了一个 switch 语句,其中每个案例都根据目标视图控制器的类型执行,并内置转换为目标类型。它是 switch 语句的一个简洁变体,在 prepare(for:) 函数中非常有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-12
        • 1970-01-01
        相关资源
        最近更新 更多