以下是我的cellForRowAtIndexPath
:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell!
if cell == nil {
cell = UITableViewCell(style: .Subtitle, reuseIdentifier: "cell")
cell.backgroundColor = UIColor.clearColor()
cell.selectionStyle = .None
cell.accessoryType = .None
cell.tag = indexPath.row
}
else {
var arr = cell.contentView.subviews
for i in 0..<arr.count {
let view = arr[i]
view.removeFromSuperview()
}
}
let people = array_people[indexPath.row]
cell.textLabel?.text = people.name
cell.detailTextLabel?.text = people.info
if let img = imageCache[people.image_url] {
cell.imageView?.image = img
}
else {
cell.imageView!.image = UIImage.gifWithName("loading")
NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: people.image_url)!, completionHandler: { (data, response, error) -> Void in
if error != nil {
print(error)
return
}
dispatch_async(dispatch_get_main_queue(), { () -> Void in
let image = UIImage(data: data!)
cell.imageView!.image = image
self.imageCache[people.image_url] = cell.imageView?.image
})
}).resume()
}
cell.imageView?.layer.cornerRadius = (cell.imageView?.frame.size.width)!/2
cell.imageView?.contentMode = .ScaleAspectFit
return cell
}
下载图像后,我在滚动UITableView
后回到它,它的大小已经改变。为什么会这样?