自定义UIImageView通过UITableViewCell重新对齐
问题描述:
我已经看到这个问题很多地方,但尚未遇到一个适合我的解决方案。我有一个自定义UITableViewCell
,其中我已经放置了一个UIImageView
。图像视图应该拥抱单元格的右侧(来自xib文件的约束)。下面是细胞是如何创建,然后格式化代码:自定义UIImageView通过UITableViewCell重新对齐
class PlaylistCell: UITableViewCell {
@IBOutlet var imView: UIImageView?
@IBOutlet var label: UILabel?
var playlist:SPTPartialPlaylist? {
didSet {
self.configure()
}
}
func configure()
{
self.imView?.clipsToBounds = true
self.label?.text = self.playlist?.name
let uri = (self.playlist?.images[0] as! SPTImage).imageURL
dispatch_async(dispatch_get_main_queue(), {
let data = NSData(contentsOfURL: uri!)
if (data != nil) {
self.imView?.image = UIImage(data: data!)
self.layoutSubviews()
}
})
}
而且在我ViewController
已在它的表视图:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("PlaylistCell") as! PlaylistCell
cell.playlist = self.playlists[indexPath.row]
cell.imView?.image = UIImage(named: "placeholder")
return cell
}
一切正确加载和细胞看起来不错,但当其中一个单元格被触摸时,图像捕捉到单元格的左侧并缩小尺寸。有谁知道为什么会发生这种情况? (PS我一直在使用SDWebImage
尝试和同一个问题随之而来)
答
你可以尝试做补充一点,在你的PlayListCell?
override func didMoveToSuperview() {
self.layoutIfNeeded()
}
发布一些截图 –