我有一个 TableView Controller ,其中填充了从 JSON 文件中提取的数据。此 TableView Controller 转至另一个从同一 JSON 文件中提取的 TableView Controller 。我希望加载到第二个 View Controller 中的信息根据在第一个 TableView Controller 中单击的表单元格进行更改。
例如:如果我的第一个 TableView Controller 列出了各州(阿拉巴马州、阿拉斯加、亚利桑那州等)并单击了阿拉巴马州,它将返回阿拉巴马州的城市列表。但是,如果单击阿拉斯加,则第二个 TableView Controller 将显示阿拉斯加的城市。
我不确定如何从这里开始,但这是我的代码优先 TableView Controller didSelectRowAtIndexPath 函数: func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { var industry: 工业!
if inSearch{
industry = filteredSearch[indexPath.row]
}
else{
industry = industryOfMifi[indexPath.row]
}
performSegueWithIdentifier("IndustryPush", sender: industry)
}
下面是在第二个 TableView Controller 中加载适当信息的代码:
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)
}
}
最佳答案
很难说清楚这里发生了什么,但本质上您要做的是将对所选 Industry 值的引用传递给第二个 TableView Controller 。
这是一种方法。首先,在第 2 个表 VC 中为 Industry 创建一个类级变量。
class SecondTableViewController: UITableViewController {
var industry: Industry?
}
其次,在第一个 Table VC 中使用 prepareForSegue() 方法将 Industry 实例传递给新的 View Controller 。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "IndustryPush" {
let secondTableViewController = segue.destinationViewController as! SecondTableViewController
secondTableViewController.industry = sender as! Industry
}
}
或者,您可以在您的第一个 VC 中创建对所选行业的引用。 (您在 didSelectRowAtIndexPath 函数中设置它。)然后您可以将其传递给 prepareForSegue() 中的第二个 VC。
从那里开始,您就可以弄清楚如何使用 Industry 实例来过滤您的 JSON。从您发布的内容中很难判断。
希望对您有所帮助。
关于ios - 根据在不同的 TableView Controller 中单击的内容转至 TableView Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40390091/