3

我已经设法使用 swiftJSON 从 JSON 中检索数据,但是当我尝试填充 tableview 时遇到了问题。我是 iOS 开发的新手,所以请多多包涵。如果您能提供帮助或提供一些想法,我将不胜感激?

这是代码:

 override func viewDidLoad(){
 super.viewDidLoad()
 getContactListJSON()
 }

 func getContactListJSON(){
let urlString = "http://jsonplaceholder.typicode.com/users"
let urlEncodedString = urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
let url = NSURL( string: urlEncodedString!)
var task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, innerError) in
    let json = JSON(data: data)
    let contactsArray = json.arrayValue

    dispatch_async(dispatch_get_main_queue(), {
        for contacts in contactsArray
        {
            let id = contacts["id"].stringValue
            let name = contacts["name"].stringValue
            println( "id: \(id) name: \(name)" )
        }
    })
}
task.resume()
}
4

3 回答 3

8

这是您的完整代码:

import UIKit

class TableViewController: UITableViewController {

var tableName = [String]()
var tableID = [String]()
override func viewDidLoad() {
    super.viewDidLoad()
    getContactListJSON()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func getContactListJSON(){
    let urlString = "http://jsonplaceholder.typicode.com/users"
    let urlEncodedString = urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
    let url = NSURL( string: urlEncodedString!)
    var task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, innerError) in
        let json = JSON(data: data)
        let contactsArray = json.arrayValue

        dispatch_async(dispatch_get_main_queue(), {
            for contacts in contactsArray
            {
                let id = contacts["id"].stringValue
                let name = contacts["name"].stringValue
                println( "id: \(id) name: \(name)" )
                self.tableName.append(name)
                self.tableID.append(id)
            }
            dispatch_async(dispatch_get_main_queue(),{
                self.tableView.reloadData()
            })
        })
    }
    task.resume()
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return tableName.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TableViewCell

    // Configure the cell...
    cell.id.text = tableID[indexPath.row]
    cell.name.text = tableName[indexPath.row]
    return cell

    }
}

HERE正在为您提供自定义单元格的示例项目。

于 2015-05-11T10:05:30.663 回答
0

这是一段代码。

var dataArray = [String]()

override func viewDidLoad(){
 super.viewDidLoad()
 getContactListJSON()
 }

 func getContactListJSON(){
let urlString = "http://jsonplaceholder.typicode.com/users"
let urlEncodedString = urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
let url = NSURL( string: urlEncodedString!)
var task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, innerError) in
    let json = JSON(data: data)
    let contactsArray = json.arrayValue

    dispatch_async(dispatch_get_main_queue(), {
        for contacts in contactsArray
        {
            let id = contacts["id"].stringValue
            let name = contacts["name"].stringValue
            println( "id: \(id) name: \(name)" )
            self.dataArray.append(name)
        }
        self.tableView.reloadData()
    })
}
task.resume()
}

我在这里取名字。如果您还想显示 id,则为其创建一个模型类。

于 2015-05-11T09:32:56.567 回答
0

首先声明一个名为contactArray 的局部变量NSArray。

    override func viewDidLoad(){
                     super.viewDidLoad()

                     getContactListJSON()


                     }


    //Code for downloading data
     func getContactListJSON(){
                        let urlString = "http://jsonplaceholder.typicode.com/users"
                        let urlEncodedString = urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
                        let url = NSURL( string: urlEncodedString!)
                        var task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, innerError) in
                            let json = JSON(data: data)
                            self.contactsArray = json.arrayValue

                            dispatch_async(dispatch_get_main_queue(), {
                                [self.tableView reloadData]
                            })
                        }
                        task.resume()
                        }

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                    return self.contactsArray.count;
                }

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
                    var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
                    var contacts:NSDictionary = self.contactsArray[indexPath.row];
                    cell.textLabel?.text =   contacts["name"].stringValue
                    //.......
                    //.......

                    return cell
                }
于 2015-05-11T09:39:27.097 回答