[将数据从一个TableViewCell传输到另一个TableView Cell][1]
我有CountriesViewController
和tableView
,其中是不同国家的列表。当我选择一个国家时,我想获得PlacesViewController
和tableView
,它显示了这个国家的不同位置。当我在CountriesViewController
中选择另一个国家时,我想获得与该国家相关的另一个地点列表。
有可能吗?
第一个视图控制器
第二视图控制器
如果有人有任何建议,可以展示一些代码示例,那将是非常棒的!
提前谢谢大家!
首先,您必须为所有数据创建一个数组,然后当点击特定行时,您将该国家的名称发送到第二个表视图,然后在其中根据国家筛选阵列。下面的代码向您展示了如何检查哪一行被点击,并将信息发送到第二个表视图:
var currentCountry : String!
class View1 : UITableViewController {
//tableview stuff
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let country = tableView.cellForRowAtIndexPath(indexPath)?.textLabel?.text //Here I use the title to define which country is clicked
currentCountry = country
}
}
class View2 : UITableViewController {
var country = currentCountry
//more tableview stuff
}
所以我想我必须实现这段代码但它只是用于将数据从具有tableView的ViewController传输到另一个没有tableView的View Controller。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "detailSegue" {
if let indexPath = tableView.indexPathForSelectedRow {
let destinationController = segue.destinationViewController as!
PlacesViewController
destinationController.placesImage =
countryImages1[indexPath.row]
}
}
}