使用plist在表视图之间传递数据

时间:2015-10-08 14:16:09

标签: ios swift uitableview plist

如何在两个表视图之间传递数据?我有poet.plist个有2个数组:namebiography。当我运行应用程序时,第二个表视图为空。第一个表视图应列出诗人,而第二个表视图列出诗人的作品。

头等舱

class Poets
{
    var poetName:String = ""
    var poetImage:String = ""
    var books=[Works]()
}

第二课:

class Works {

    var nameWork = ""
    var workPoet = ""

    init(nameWork: String, workPoet: String) {
        self.nameWork = nameWork
        self.workPoet = workPoet
    }
}

第一视图控制器

let worksArray = booksDict[poet.poetName as String] as! NSArray
for work in worksArray {
    let dictionaryFromArray = work as! NSDictionary
    let workObject = Works(nameWork: dictionaryFromArray["nameWork"] as! String, workPoet: dictionaryFromArray["work"] as! String)
    poet.books.append(workObject)
}

如何在诗人姓名的UITableViewController和诗人的作品UITableViewController之间划一句话?

1 个答案:

答案 0 :(得分:2)

在第一个视图控制器

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    // ShowPoetWorks is your segue identifier which you can set in storyboard
    if segue.identifier == "ShowPoetWorks" {

        var secondVC = segue.destinationViewController as? SecondViewController
        secondVC.poetName = poetName
    }
}

在第二视图控制器

  • 定义2个字段:poetNameworkCount
  • 覆盖函数`tableView:numberOfRowsInSection

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
        return workCount  
    }
    
  • 在viewDidLoad中,加载poet基于poetName工作并调用tableView.reloadData()