我試圖在桌面視圖中顯示來自api的圖像,但我沒有得到圖像或任何東西。 ImageTableViewCell只有圖像的出口。在UITableview中顯示圖像
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()
}
}
}
}
}
任何幫助將不勝感激。我嘗試了很多方法,但這似乎很簡單,並且易於跟蹤
在這一行你的應用程序崩潰了? self.images.append(album [「pic」] as!String) – KKRocks
yes你是對的@KKRocks – leo0019
這行是什麼:self.images.append(album [「pic」] as!字符串) – KKRocks