根据选择的初始值显示JSON数据

时间:2016-02-02 00:45:42

标签: ios json swift segue swifty-json

我有一个JSON文件(当前是本地的,但将是一个及时的URL),包含术语,描述和ID。我想要实现的是在我的初始视图中列出所有术语,然后根据所选择的内容,显示一个新视图(通过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"
    }

}

任何建议都将不胜感激!

1 个答案:

答案 0 :(得分:1)

根据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   
        }
    }
}