이미지가 너비에서 imageView를 채우고 이미지가 종횡비를 유지하도록 셀의 높이를 동적으로 변경하고 싶습니다.
나는 이것을 사용하여 시도하고 UITableViewAutomaticDimension
있습니다. 온라인에서 사용할 수있는 튜토리얼에 따르면 이것은 간단한 작업 인 것 같습니다. 하지만 제 경우에는 셀의 높이가이 작업을 수행하도록 조정되지 않습니다. 이미지가 가로 세로 비율로 채워져 많은 너비 공간을 비워 두거나 너비를 채우지 만 이미지의 일부를 세로로 자릅니다. 셀 뷰와 관련하여 선행, 후행, 상단 및 하단 제약 조건이 있는지 확인 tableView.rowHeight = UITableViewAutomaticDimension
하고 viewDidLoad에 table.estimatedRowHeight가 설정되어 있는지 확인했습니다 . 이 링크에 프로젝트를 업로드했습니다. 누군가 내가 여기서 놓칠 수있는 것을 제안 해 주시겠습니까?
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var picTableView: UITableView!
let reuseidentifier = "dynamicCellTableViewCell"
let images = [UIImage(named: "feedImage1"), UIImage(named: "feedImage2"), UIImage(named: "feedImage3")]
override func viewDidLoad() {
super.viewDidLoad()
picTableView.rowHeight = UITableViewAutomaticDimension
picTableView.estimatedRowHeight = 1000
}
// MARK: Table View Data Source
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return images.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(reuseidentifier, forIndexPath: indexPath) as! DynamicCellTableViewCell
cell.displayImageView.image = images[indexPath.row]
return cell
}
}
나는 해결책을 찾았다. UITableViewAutomaticDimension과 함께 사용할 때 UIImageView는 크기로 UIImage의 실제 너비와 높이 만 찾습니다. 따라서 UIImageView는 레이아웃 제약과 일치하도록 크기 조정 된 너비 (aspectFit)의 크기 조정 된 높이가 아니라 UIImage의 원래 높이에 응답하도록 높이를 조정합니다.
따라서 트릭은 먼저 이미지의 너비와 높이를 셀에 필요한 너비와 높이와 일치하도록 변경하는 것입니다. 나머지 작업은 UITableViewAutomaticDimension에 의해 수행됩니다. 아래는 현재 작동하는 업데이트 된 cellForRowAtIndex 함수입니다. 귀하의 의견을 보내 주셔서 감사합니다.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(reuseidentifier, forIndexPath: indexPath) as! DynamicCellTableViewCell
let image = images[indexPath.row]!
let newWidth = cell.displayImageView.frame.width
let scale = newWidth/image.size.width
let newHeight = image.size.height * scale
UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight))
image.drawInRect(CGRectMake(0, 0, newWidth, newHeight))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
cell.displayImageView.image = newImage
return cell
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다