[英]Image are not showing in table View
我有一個JSON鏈接,我正在使用Alamofire將數據解析到表格視圖中。 我從Json得到了Text,但是圖像沒有顯示。
導入UIKit導入Alamofire導入AlamofireImage
ViewController類:UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet var tableView: UITableView!
var newsArray = [AnyObject]()
override func viewDidLoad() {
super.viewDidLoad()
此處稱為JSON
Alamofire.request("http://iccukapp.org/Api_json_format").responseJSON { response in
let result = response.result
if let dict = result.value as? Dictionary<String,AnyObject>{
if let innerDict = dict["result"]{
self.newsArray = innerDict as! [AnyObject]
self.tableView.reloadData()
}
}
}
}
此處數據已加載到表格視圖中
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return newsArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCell
let title = newsArray[indexPath.row]["title"]
cell.newsLabel.text = title as? String
這個網址是我的圖片網址,但如果我查看JSON,則圖片名稱前沒有網址。 這是我的json鏈接http://iccukapp.org/Api_json_format 。 我有圖像資產鏈接。 鏈接在這里http://iccukapp.org/assets/admin/images/在此處輸入鏈接描述
有什么辦法可以將外部鏈接添加到我的網址?
let url = NSURL(string: self.newsArray[indexPath.row]["post_iamge"]! as! String)
cell.imageVieww.af_setImage(withURL: url! as URL, placeholderImage: #imageLiteral(resourceName: "loadig.gif"), filter: nil, imageTransition: .crossDissolve(0.5), runImageTransitionIfCached: true, completion: nil)
return cell
}
}
先進的謝謝。
我認為您需要將圖像名稱附加到圖像資產鏈接,然后使用該URL來顯示圖像。 嘗試這個
let imageUrl = "http://iccukapp.org/assets/admin/images/" + (self.newsArray[indexPath.row]["post_iamge"]! as! String)
let url = NSURL(string:imageUrl)
cell.imageVieww.af_setImage(withURL: url! as URL, placeholderImage: #imageLiteral(resourceName: "loadig.gif"), filter: nil, imageTransition: .crossDissolve(0.5), runImageTransitionIfCached: true, completion: nil)
您需要在服務器URL后面附加圖像字符串。
let fullImageUrl = "your serverurl" + (self.newsArray[indexPath.row]["post_iamge"]! as! String)
let url = URL(string: fullImageUrl) as URL
cell.imageVieww.af_setImage(withURL: url!, placeholderImage: #imageLiteral(resourceName: "loadig.gif"), filter: nil, imageTransition: .crossDissolve(0.5), runImageTransitionIfCached: true, completion: nil)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.