Display images in UITableview

送分小仙女□ 提交于 2019-12-12 06:49:15

问题


I'm trying to display images from api in the tableview but I get no images or anything. ImageTableViewCell has the outlet to the image only.

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()
                }

    }
  }
   }

   }

Any help will be appreciated. I tried many methods but this seems to be simple and easy to follow up


回答1:


You must have at least one section to show.

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



回答2:


Check below code

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()

            }
    }
}



回答3:


Your JSON response is Dictionary not Array and its contains pic array inside of it. So type result of jsonObject(with:options:) to [String:Any] instead of [[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()
}


来源:https://stackoverflow.com/questions/43717507/display-images-in-uitableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!