【问题标题】:Load JSON into UItableView in Swift在 Swift 中将 JSON 加载到 UItableView
【发布时间】:2015-05-09 19:08:22
【问题描述】:

我一直在尝试从 JSON 返回字符串 url 并将其存储在数组中,然后在 UITableView 中显示该数组。但它显示为空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 期间,它返回看起来像的网址(如下),但是当我尝试在UITableView 中显示它时,它总是变为空UILabel 所以我在这里做错了什么?:

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

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

【问题讨论】:

  • 也许是cell.nameLabel.text = TAG_IMG_URL[indexPath.row] as? NSString
  • 它说“不能将 NSString 类型的值分配给 String 类型的值?”
  • 尝试在cellForRowAtIndexPath方法中打印到控制台TAG_IMG_URL[indexPath.row]
  • 它打印( "https://lh3.googleusercontent.com/-uubi7qNJ2Kw/VIbLAn8TlJI/AAAAAAAAAOM/gsv76rQICtQ/IMG_20140523_170813.jpg" ) ( "https://lh3.googleusercontent.com/-q-vWgMdlEfU/VIbLHRMWikI/AAAAAAAAAOM/u4QP7_o68E8/IMG_20140628_152242.jpg" ) 如何仅从该数组中获取字符串!
  • 那么也许你的问题出在你的故事板文件中,你是否设置了正确的单元标识符和单元类?

标签: ios json uitableview swift


【解决方案1】:

试试这个:

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]

【讨论】:

    猜你喜欢
    • 2016-12-28
    • 1970-01-01
    • 2018-12-23
    • 2019-08-01
    • 1970-01-01
    • 2019-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多