[英]How to load image from server to tableView cell in swift 3?
I am trying to load news into the tableView and I did it.我正在尝试将新闻加载到 tableView 中,我做到了。 How do I load an image to the tableView cell next to the title?如何将图像加载到标题旁边的 tableView 单元格?
I want the image to be loaded at the left side of the title.我希望在标题的左侧加载图像。
host: https://api.sis.kemoke.net/news主持人: https : //api.sis.kemoke.net/news
image URL variable: imgUrl图片 URL 变量:imgUrl
Here is my class:这是我的课:
import Alamofire //Framework for handling http requests
import UIKit
/*A class which will process http requests, receive the news as string and fill the array with such data which will be displayed in a table*/
class NewsTableViewController: UITableViewController {
//Custom struct for the data
struct News {
let title : String
let text : String
let link : String
init(dictionary: [String:String]) {
self.title = dictionary["title"] ?? ""
self.text = dictionary["text"] ?? ""
self.link = dictionary["link"] ?? ""
}
}
//Array which holds the news
var newsData = [News]()
// Download the news
func downloadData() {
Alamofire.request("https://api.sis.kemoke.net/news").responseJSON { response in
print(response.request) // original URL request
print(response.response) // HTTP URL response
print(response.data) // server data
print(response.result) // result of response serialization
//Optional binding to handle exceptions
self.newsData.removeAll() // clean the data source array
if let json = response.result.value as? [[String:String]] {
for news in json {
self.newsData.append(News(dictionary: news))
}
self.tableView.reloadData()
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
downloadData()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return newsData.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let news = newsData[indexPath.row]
cell.textLabel?.text = news.title
return cell
}
}
Somehow the "best practice" to do something like this, is to create a custom cell (UITableViewCell) and a model class for each news.以某种方式执行此类操作的“最佳实践”是为每个新闻创建一个自定义单元格 (UITableViewCell) 和一个模型类。 this scenario is something that every "swift tutorial site" have some example of it.这种情况是每个“快速教程站点”都有的例子。 you can find some of them in these links:您可以在以下链接中找到其中一些:
http://www.kaleidosblog.com/swift-uitableview-load-data-from-json http://www.kaleidosblog.com/swift-uitableview-load-data-from-json
https://grokswift.com/uitableviewcell-images-from-api/ https://grokswift.com/uitableviewcell-images-from-api/
Swift 3 This code is tableview using image and label print in tableview cell get the api Json parse data Swift 3 此代码是tableview,在tableview 单元格中使用图像和标签打印获取api Json 解析数据
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell表视图单元格
var dict = array[indexPath.row]
cell.lbl1.text = dict["address"] as? String
cell.lbl2.text = dict["ad_created_date"] as? String
cell.lbl3.text = dict["phone_number"] as? String
cell.lbl4.text = dict["id"] as? String
cell.lbl5.text = dict["ad_zip"] as? String
let imageUrlString = dict["ad_path"]
let imageUrl:URL = URL(string: imageUrlString as! String)!
let imageData:NSData = NSData(contentsOf: imageUrl)!
cell.img.image = UIImage(data: imageData as Data)
return cell
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.