<分区>
我有这段代码应该显示来自 URLS 的图像。我认为一切都很好,因为当我打印图像和 images
的计数时,我得到了 7 张图像的数组。
请查看我的代码并纠正我做错的地方。
import UIKit
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 remote = images[indexPath.row]
let imageurl = URL(string: remote)
cell.images.sd_setImage(with: imageurl)
return cell
}
func getJSON() {
let url = "http://localhost:8000/api/hello"
let myuel = URL(string: url)
let resquest = NSMutableURLRequest(url: myuel!)
resquest.httpMethod = "GET"
let task = URLSession.shared.dataTask(with: resquest as URLRequest, completionHandler: { data,response,error in
if error != nil{
print(error!.localizedDescription)
return
}
let json = JSON(data)
self.images = json["pic"].arrayObject! as! [String]
print(self.images.count)
print(self.images)
DispatchQueue.main.async {
self.tableview.reloadData()
}
})
task.resume()
}
}