我有一个表视图控制器,其中填充了从JSON文件中提取的数据 . 此表视图控制器将切换到另一个从同一JSON文件中提取的表视图控制器 . 我希望加载到第二个视图控制器的信息根据第一个表视图控制器中单击的表格单元格进行更改 .
例如:如果我的第一个表视图控制器列出状态(阿拉巴马州,阿拉斯加州,亚利桑那州等)和阿拉巴马州被点击,它将返回阿拉巴马州的城市列表 . 但是,如果单击阿拉斯加,则第二个表视图控制器将显示位于阿拉斯加的城市 .
我不确定如何从这里开始,但这里是我的代码第一个表视图控制器didSelectRowAtIndexPath函数:func tableView(tableView:UITableView,didSelectRowAtIndexPath indexPath:NSIndexPath){var industry:Industry!
if inSearch{
industry = filteredSearch[indexPath.row]
}
else{
industry = industryOfMifi[indexPath.row]
}
performSegueWithIdentifier("IndustryPush", sender: industry)
}
以下是在第二个表视图控制器中加载适当信息的代码:
func parseJSON(){
do{
let data = NSData(contentsOfURL: NSURL(string: "https://jsonblob.com/api/jsonBlob/580d0ccce4b0bcac9f837fbe")!)
let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)
for anItem in jsonResult as! [Dictionary<String, AnyObject>]{
let industry = anItem["mediaIndustry"] as! String
if industry == "Interactive Media" {
let mifiIndustry = anItem["name"] as! String
print(mifiIndustry)
let mifiId = anItem["employeeId"] as! Int
let newIndustry = Name(mifiName: mifiIndustry, mifiId: mifiId)
industryOfMifi.append(newIndustry)
}
else if industry == "Newspaper" {
let mifiIndustry = anItem["name"] as! String
print(mifiIndustry)
let mifiId = anItem["employeeId"] as! Int
let newIndustry = Name(mifiName: mifiIndustry, mifiId: mifiId)
industryOfMifi.append(newIndustry)
}
else if industry == "Radio" {
let mifiIndustry = anItem["name"] as! String
print(mifiIndustry)
let mifiId = anItem["employeeId"] as! Int
let newIndustry = Name(mifiName: mifiIndustry, mifiId: mifiId)
industryOfMifi.append(newIndustry)
}
}
}
catch let error as NSError{
print(error.debugDescription)
}
}
1 回答
告诉这里发生了什么有点困难,但实际上你想要做的是将对所选Industry值的引用传递给第二个Table View Controller .
这是一种方法 . 首先,在第二个表VC中为Industry创建一个类级变量 .
其次,在第一个表VC中使用prepareForSegue()方法将Industry实例传递给新的视图控制器 .
或者,您可以在第一个VC中创建对所选行业的引用 . (您可以在didSelectRowAtIndexPath函数中设置它 . )然后您可以将THAT传递给prepareForSegue()中的第二个VC .
从那里开始,您可以了解如何使用Industry实例过滤您的JSON . 你发布的内容有点难以辨别 .
希望有所帮助 .