【发布时间】:2016-07-07 20:41:18
【问题描述】:
我有一个UITableView 填充有一个JSON,它通过Alamofire 排出,在JSON 中有我需要下载每个单元格的图像的URL。我的问题是,一旦我下载了图像,当他扔掉图表图像时会重新下载,并且单元格有错误的图像,直到它完成下载正确的图像。如何获取下载后仍存储在正确单元格中的图像?
这是我的代码:
override func viewDidLoad() {
super.viewDidLoad()
self.refreshTable()
}
func refreshTable(){
Alamofire.request(.GET, urlComponent, parameters: nil, encoding:.JSON).responseJSON
{ response in switch response.result {
case .Success(let JSON):
let data = JSON as! Dictionary<String, AnyObject>
for (_,value) in data{
let object = value as! NSMutableArray
for item in object{
self.arrayData.addObject(item)
}
}
self.tableView.reloadData()
case .Failure(let error):
print("Request failed with error: \(error)")
}
}
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if(self.arrayData.count != 0){
return self.arrayData.count
}
else{
return 0
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CollectionCell
if(self.arrayData.count != 0){
let dizionario = self.arrayData.objectAtIndex(indexPath.row) as! NSDictionary
cell.titolo.text = dizionario.objectForKey("title") as? String
cell.code.text = dizionario.objectForKey("code") as? String
let url = NSURL(string:dizionario.objectForKey("img") as! String)!
Alamofire.request(.GET, url, parameters: nil).responseData
{ response in switch response.result {
case .Success(let JSON):
let data = JSON
cell.immagine.image = UIImage(data: data)
case .Failure(let error):
print("Request failed with error: \(error)")
}
}
}
return cell
}
【问题讨论】:
标签: ios json image uitableview alamofire