在 UITableview 中显示图像 [英] Display images in UITableview

查看:30
本文介绍了在 UITableview 中显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

您必须至少有一个部分要显示.

You must have at least one section to show.

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

这篇关于在 UITableview 中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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