在Swift中将JSON加载到UItableView中

我一直试图从JSON返回stringurl,并将其存储在数组中,然后在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的正确数量。

尝试这个:

 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 } 

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