【发布时间】:2018-06-11 16:47:52
【问题描述】:
我已将 UIImageView 放置在我的自定义单元格中(Swift 3):
let cell = self.tableView.dequeueReusableCell(withIdentifier: "Feedcell", for: indexPath) as! FruitTableViewCell
let hero: List_Feed
hero = heroes[indexPath.row]
if(hero.photo==""){
cell.cnHeight.constant = 0
cell.cnTopMargin.constant = 0
cell.cnBottomMargin.constant = 52
cell.imgPhoto.image = UIImage()
cell.imgPhoto.frame = CGRect(x: 0, y: 0, width: 0, height: 0)
}else{
cell.cnTopMargin.constant = 14
cell.cnBottomMargin.constant = 50
cell.imgPhoto.frame = CGRect(x: 0, y: 0, width: 0, height: 0)
//displaying image
Alamofire.request(hero.photo!).responseImage { response in
debugPrint(response)
if let image = response.result.value {
//let newWidth = containerView.frame.height * ratio
let bounds = UIScreen.main.bounds
let width = bounds.size.width
let imagewidth = image.size.width
let imageheight = image.size.height
let ot1 = width / imagewidth
let ot2 = round(ot1 * imageheight)
//let ot3 = round(ot2 / 2)
//let ot4 = ot3 - 12
//let ot5 = ot4 + 50
cell.cnHeight.constant = ot2
cell.imgPhoto.image = image
}
}
}
这是我向下滚动然后向上滚动时的显示方式:
【问题讨论】:
-
你用的是autolaout,那为什么
cell.imgPhoto.frame -
在不断变化之后,你在做 layoutIfNeeded() 吗?
-
为什么当单元格显示时你一次又一次地下载图像? .你应该使用SDWebImage库或者alamofire图片库来缓存图片。
标签: ios swift uitableview uiimageview