저는 UITableView가 있고 각 사용자 정의 셀 (UIImageView 사용)마다 다른 크기의 다른 이미지를 표시해야합니다. 제대로 표시하려면 각 이미지의 종횡비를 계산하고 UIImageView의 프레임을 조정해야합니다. 그러나 문제가 있습니다. 앱을 실행하면 모든 셀에 대해 잘못된 종횡비가 표시됩니다. 그런 다음 나는 테이블의 맨 아래로 슬라이딩하기 시작하고 다시 몇 번 위로 가기 시작합니다. 그리고 때마다 나는 올바른 종횡비를 보았고, 어떤 세포에서는 다른 것이고, 다른 세포에서는 잘못되었다. 시스템이 프로토 타입 셀을 재사용하고 있기 때문입니까?UITableViewCell, 이미지 및 종횡비
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("EntryCell", forIndexPath: indexPath) as? PodCell
let podcast = podList[indexPath.row]
let title = podcast.title
// cell.titleLa?.text = title
cell?.titleLabel.lineBreakMode = .ByWordWrapping
cell?.titleLabel.numberOfLines = 0
cell?.titleLabel.text = title
var image = UIImage()
if (imageCache[podcast.imageURL!] != nil) {
image = imageCache[podcast.imageURL!]!
cell?.imgView.image = image
}else{
let imgURL = NSURL(string: podcast.imageURL!)
let request = NSURLRequest(URL: imgURL!)
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config)
let task = session.dataTaskWithRequest(request) { (data: NSData?, response: NSURLResponse?, error:NSError?) in
if error == nil {
if data != nil {
image = UIImage(data: data!)!
}
dispatch_async(dispatch_get_main_queue(), {
self.imageCache[podcast.imageURL!] = image
cell?.imgView.image = image
})
}else {
print(error)
}
}
task.resume()
}
let aspectRatio = image.size.width/image.size.height
cell?.imgView.frame = CGRectMake((cell?.imgView.frame.origin.x)!, (cell?.imgView.frame.origin.y)!, (cell?.imgView.frame.width)!, (cell?.imgView.frame.width)!/aspectRatio)
return cell!
}
상수 값은 너비입니다. 셀 너비가 너비와 같아야합니다. – ttrs