【发布时间】:2016-07-08 03:27:41
【问题描述】:
我的单元格图像有问题。我在下面描述它。
具有包含 1000 个单元格的表格的屏幕。
每个单元由 a) 图像 b) 标签组成。
图片和标签数据来自json。
通过使用 AFNetwork,我获取 json 并绑定标签和图像。问题出在图像上。标签完美结合。从 json 图像 url 也正确出现。但是在开始滚动时显示以前的单元格图像。
第一次滚动未开始时,所有图像都完美显示。但是在滚动问题开始并显示以前的单元格图像之后。
代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if self.arrPostImage![indexPath.row] as! String == ""
{
let cell1: CellNewNoImage = tableView.dequeueReusableCellWithIdentifier("cell2") as! CellNewNoImage
return cell1
}
else
{
let cell1: CellForMyActivityMainTableView = tableView.dequeueReusableCellWithIdentifier("cell") as! CellForMyActivityMainTableView
cell1.title.text = self.arrTitle![indexPath.row] as? String
cell1.postDate.text = self.arrPostDate![indexPath.row] as? String
cell1.postDescription.text = self.arrPostDescription![indexPath.row] as? String
//=========== Problem Start ====================
self.arrPostPersonImg![indexPath.row] = self.arrPostPersonImg![indexPath.row].stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
let imgPath = self.arrPostPersonImg![indexPath.row]
let url12 = NSURL(string:imgPath as! String )
getDataFromUrl(url12!) { (data, response, error) in
dispatch_async(dispatch_get_main_queue()) { () -> Void in
guard let data = data where error == nil else { return }
cell1.postPersonImg.image = UIImage(data: data)
}
}
self.arrPostImage![indexPath.row] = self.arrPostImage![indexPath.row].stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
let imgPath2 = self.arrPostImage![indexPath.row]
let url122 = NSURL(string:imgPath2 as! String )
if let url = NSURL(string:imgPath2 as! String) {
if let data = NSData(contentsOfURL: url) {
cell1.postImage.image = UIImage(data: data)
}
}
return cell1
}
}
func getDataFromUrl(url:NSURL, completion: ((data: NSData?, response: NSURLResponse?, error: NSError? ) -> Void))
{
NSURLSession.sharedSession().dataTaskWithURL(url)
{ (data, response, error) in
completion(data: data, response: response, error: error)
}.resume()
}
【问题讨论】: