根据选择的初始值显示 JSON 数据
Display JSON data based on initial value selected
我有一个包含术语、描述和 ID 的 JSON 文件(当前是本地文件,但将来会是 URL)。我想要实现的是在我的初始视图中列出所有术语,然后根据所选择的内容,显示一个新视图(通过 segue),其中显示术语 + 它的相关描述。
在我的初始视图控制器中,我有一个 TableView 当前显示 JSON 中的术语列表。我已经设法将选定的术语传递到新视图,但我在显示相关描述时遇到了困难。我正在使用 SwiftyJSON,我的解析器看起来像:
func parseJSON(){
let path : String = NSBundle.mainBundle().pathForResource("scrumTerms", ofType: "json") as String!
let jsonData = NSData(contentsOfFile: path) as NSData!
let readableJSON = JSON(data: jsonData, options: NSJSONReadingOptions.MutableContainers, error: nil)
NumberofRows = readableJSON["Terms"].count
for i in 1...NumberofRows{
var item = "item"
item += "\(i)"
let term = readableJSON["Terms"][item]["term"].string as String!
let description = readableJSON["Terms"][item]["description"].string as String!
let id = readableJSON["Terms"][item]["id"].int as Int!
TermsArray.append(term)
DescriptionArray.append(description)
//IdArray.append(id)
}
初始列表显示如下:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell!
if TermsArray.count != 0{
cell.textLabel?.text = TermsArray[indexPath.row]
}
return cell
}
然后我的 segue 部分看起来像:
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
// Get Cell Label
let indexPath = tableView.indexPathForSelectedRow!
let currentCell = tableView.cellForRowAtIndexPath(indexPath)! as UITableViewCell
viewTerm = currentCell.textLabel!.text
performSegueWithIdentifier("viewTerm", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
if (segue.identifier == "viewTerm") {
// define new view controller
var viewController = segue.destinationViewController as! viewTermController
// define the new view controller property that will store the passed value(s)
viewController.viewTerm = viewTerm
viewController.viewDescription = "THIS IS WHERE I NEED TO GET TO"
}
}
如有任何建议,我们将不胜感激!
根据 Michael 的初始评论以及使用 SwitfyJSON 使用替代方法(字典)成功实施。谢谢
我最终宣布我的 JSON 为:
let path: String = NSBundle.mainBundle().pathForResource("MY FILE NAME", ofType: "json") as String!
let data = NSData(contentsOfFile: path) as NSData!
let json = JSON(data: data)
parseJSON(json)
然后解析函数看起来像:
func parseJSON(json: JSON) {
for result in json["results"].arrayValue {
let title = result["keyTerm"].stringValue
let desc = result["description"].stringValue
let termId = result["termId"].stringValue
let obj = ["keyTerm": title, "description": desc, "termId": termId]
objects.append(obj)
}
tableView.reloadData()
}
然后我的 segue 将所选行的值传递给第二个视图控制器:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
if segue.identifier == "showDetail" {
if let indexPath = self.tableView.indexPathForSelectedRow {
let object = objects[indexPath.row]
let controller = segue.destinationViewController as! DetailViewController
controller.detailItem = object
}
}
}
我有一个包含术语、描述和 ID 的 JSON 文件(当前是本地文件,但将来会是 URL)。我想要实现的是在我的初始视图中列出所有术语,然后根据所选择的内容,显示一个新视图(通过 segue),其中显示术语 + 它的相关描述。
在我的初始视图控制器中,我有一个 TableView 当前显示 JSON 中的术语列表。我已经设法将选定的术语传递到新视图,但我在显示相关描述时遇到了困难。我正在使用 SwiftyJSON,我的解析器看起来像:
func parseJSON(){
let path : String = NSBundle.mainBundle().pathForResource("scrumTerms", ofType: "json") as String!
let jsonData = NSData(contentsOfFile: path) as NSData!
let readableJSON = JSON(data: jsonData, options: NSJSONReadingOptions.MutableContainers, error: nil)
NumberofRows = readableJSON["Terms"].count
for i in 1...NumberofRows{
var item = "item"
item += "\(i)"
let term = readableJSON["Terms"][item]["term"].string as String!
let description = readableJSON["Terms"][item]["description"].string as String!
let id = readableJSON["Terms"][item]["id"].int as Int!
TermsArray.append(term)
DescriptionArray.append(description)
//IdArray.append(id)
}
初始列表显示如下:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell!
if TermsArray.count != 0{
cell.textLabel?.text = TermsArray[indexPath.row]
}
return cell
}
然后我的 segue 部分看起来像:
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
// Get Cell Label
let indexPath = tableView.indexPathForSelectedRow!
let currentCell = tableView.cellForRowAtIndexPath(indexPath)! as UITableViewCell
viewTerm = currentCell.textLabel!.text
performSegueWithIdentifier("viewTerm", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
if (segue.identifier == "viewTerm") {
// define new view controller
var viewController = segue.destinationViewController as! viewTermController
// define the new view controller property that will store the passed value(s)
viewController.viewTerm = viewTerm
viewController.viewDescription = "THIS IS WHERE I NEED TO GET TO"
}
}
如有任何建议,我们将不胜感激!
根据 Michael 的初始评论以及使用 SwitfyJSON 使用替代方法(字典)成功实施。谢谢
我最终宣布我的 JSON 为:
let path: String = NSBundle.mainBundle().pathForResource("MY FILE NAME", ofType: "json") as String!
let data = NSData(contentsOfFile: path) as NSData!
let json = JSON(data: data)
parseJSON(json)
然后解析函数看起来像:
func parseJSON(json: JSON) {
for result in json["results"].arrayValue {
let title = result["keyTerm"].stringValue
let desc = result["description"].stringValue
let termId = result["termId"].stringValue
let obj = ["keyTerm": title, "description": desc, "termId": termId]
objects.append(obj)
}
tableView.reloadData()
}
然后我的 segue 将所选行的值传递给第二个视图控制器:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
if segue.identifier == "showDetail" {
if let indexPath = self.tableView.indexPathForSelectedRow {
let object = objects[indexPath.row]
let controller = segue.destinationViewController as! DetailViewController
controller.detailItem = object
}
}
}