-1

我试图在 tableview 中显示来自 api 的图像,但我没有得到任何图像或任何东西。ImageTableViewCell 只有图像的出口。

import UIKit
import Alamofire
import SwiftyJSON
import Haneke

class  SlideViewController: UIViewController , UITableViewDelegate , UITableViewDataSource {

@IBOutlet weak var tableview : UITableView!

 var images = [String]()




override func viewDidLoad() {
    super.viewDidLoad()

    tableview.delegate = self
    tableview.dataSource = self

    getJSON()


}


func numberOfSections(in tableView: UITableView) -> Int {
    return 0
}


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



 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "imageCell", for: indexPath) as! ImageTableViewCell


    let url = URL(string: images[indexPath.row])

    cell.images.hnk_setImage(from: url!)

    return cell
}




func getJSON() {

    let url =  "http://localhost:8000/api/hello"
    request(url ,  method: .get,  encoding: JSONEncoding.default  )
        .responseJSON { response in

            if let value: AnyObject = response.result.value as AnyObject? {
                //Handle the results as JSON
                do{
                    if let albums = try JSONSerialization.jsonObject(with: response.data!, options: []) as? [String: Any],
                        let pics = albums["pic"] as? [Any] {

                        self.images = pics as! [String]

                        for kick in pics {
                            self.images.append(kick as! String)

                        }

                    }

                }catch {
                    print("Error with Json: \(error)")
                }
                DispatchQueue.main.async {
                    self.tableview.reloadData()
                }

    }
  }
   }

   }

任何帮助将不胜感激。我尝试了很多方法,但这似乎很简单,易于跟进

4

3 回答 3

1

您的 JSON 响应Dictionary不是Array,它包含其中pic的数组。所以键入结果jsonObject(with:options:)[String:Any]不是[[String: AnyObject]]

do{
    if let albums = try JSONSerialization.jsonObject(with: response.data!, options: []) as? [String: Any], 
         let pics = albums["pics"] as? [Any] {

        images = pics.flatMap{ $0 as? String }
    }
}catch {
    print("Error with Json: \(error)")
}
DispatchQueue.main.async {
    self.tableview.reloadData()
}
于 2017-05-01T10:30:02.740 回答
1

检查下面的代码

var images = [String]()

func getJSON() {

    let url =  "http://localhost:8000/api/hello"
    request(url ,  method: .get,  encoding: JSONEncoding.default  )
        .responseJSON { response in

            if let value: AnyObject = response.result.value as AnyObject? {
                //Handle the results as JSON
                do{
                    let albums = try JSONSerialization.jsonObject(with: response.data!, options:.allowFragments) as! [[String: AnyObject]]
                    print(albums)
                    if let pics = album["pic"]
                    {
                       for album in pics
                       {
                            images.append(album)
                       }
                    }

                }catch {
                    print("Error with Json: \(error)")
                }
                self.tableview.reloadData()

            }
    }
}
于 2017-05-01T10:37:16.973 回答
0

您必须至少显示一个部分。

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}
于 2017-05-02T13:50:11.560 回答