在 Swift 中将 JSON 加载到 UItableView 中 [英] Load JSON into UItableView in Swift

查看:36
本文介绍了在 Swift 中将 JSON 加载到 UItableView 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试从 JSON 返回字符串 url 并将其存储在数组中,然后在 UITableView 中显示该数组.但它显示空 UILabel.

I've been trying to return string urls from JSON and store it in array and then show the array in UITableView. but it shows empty UILabel.

class PhotosTableViewController: UITableViewController {


let imageLoadURL = "https://..."
    var TAG_IMG_URL = []


    verride func viewDidLoad() {
            super.viewDidLoad()

                getLatestPhotos()

        }

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

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

        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

            let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! KivaLoanTableViewCell
              cell.nameLabel.text = TAG_IMG_URL[indexPath.row] as? String
            return cell
        }

        func getLatestPhotos() {
                let request = NSURLRequest(URL: NSURL(string: imageLoadURL)!)
                let urlSession = NSURLSession.sharedSession()
                let task = urlSession.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in


                    if error != nil {
                        println(error.localizedDescription)
                    }

                    self.TAG_IMG_URL = self.parseJsonData(data)
                    println("\(self.TAG_IMG_URL.count)")

                    dispatch_async(dispatch_get_main_queue(), {
                        self.tableView.reloadData()
                    })

                })

                task.resume()
            }


            func parseJsonData(data: NSData) -> NSArray {

                    var error:NSError?

                    let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &error) as? NSDictionary

                    if error != nil {
                        println(error?.localizedDescription)
                    }

                    if let j = jsonResult, let mediaObjects = j.valueForKeyPath("feed.entry.media$group.media$content") as? NSArray {

                        if let imageUrls: AnyObject = mediaObjects.valueForKey("url") {
                           TAG_IMG_URL = imageUrls as! NSArray
                        }
                    }
                    println("\(TAG_IMG_URL)")


                    self.alert.dismissWithClickedButtonIndex(0, animated: true)

                    return TAG_IMG_URL

                }

}

parseJsonData 期间,它返回它看起来像的 url(如下),但是当我尝试在 UITableView 中显示它时,它总是变为空 UILabel 那么我在这里做错了什么?:

During parseJsonData it returns the urls which it look like (below), but when i try to show it in the UITableView it always becomes empty UILabel so what am i doing wrong here ?:

(
        (
        "https://..."
    ),
        (
        "https://..."
    )
)

注意:在 numberOfRowsInSection 中,它返回其 2 个 url 的正确数量.

Note : in numberOfRowsInSection it returns the right amount which its 2 urls.

推荐答案

试试这个:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

            let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! KivaLoanTableViewCell
              cell.nameLabel.text = TAG_IMG_URL[indexPath.row][0] as? String
            return cell
        }

问题是你有二维数组,所以你应该得到对象中的第一个对象:TAG_IMG_URL[indexPath.row].firstObjectTAG_IMG_URL[indexPath.row][0].

The problem that you have 2-dimensional array, so you should get first object in object: TAG_IMG_URL[indexPath.row].firstObject or TAG_IMG_URL[indexPath.row][0].

这篇关于在 Swift 中将 JSON 加载到 UItableView 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆