Reputation: 1665
In UITablView For rows that contain an image, how to figure out an appropriate height.
For the other rows in table, one can just let them automatically figure their own. I have a scrollview in the cell that dynamically adds imageView once i get the image from the url.
height (using autolayout) by returning UITableViewAutomaticDimension from heightForRowAtIndexPath.
code for my imageViewCell is give below
class ImageTableViewCell: UITableViewCell {
var imageURL: NSURL? {
didSet {
cellImage = nil
fetchImage()
}
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return imageView
}
@IBOutlet weak var scrollView: UIScrollView!{
didSet {
scrollView.contentSize = cellImageView.frame.size
scrollView.minimumZoomScale = 0.03
scrollView.maximumZoomScale = 1.0
scrollView.zoomScale = 1.0;
}
}
@IBOutlet weak var spinner: UIActivityIndicatorView!
private func fetchImage() {
if let url = imageURL {
spinner?.startAnimating()
let qos = Int(QOS_CLASS_USER_INITIATED.value)
dispatch_async(dispatch_get_global_queue(qos, 0)) {
() -> Void in
let imageData = NSData(contentsOfURL: url)
dispatch_async(dispatch_get_main_queue()) {
if url == self.imageURL {
if imageData != nil {
self.cellImage = UIImage(data: imageData!)
} else {
self.cellImage = nil
}
}
}
}
}
}
private var cellImageView = UIImageView()
private var cellImage: UIImage? {
get { return cellImageView.image }
set {
cellImageView.image = newValue
cellImageView.sizeToFit()
scrollView?.contentSize = cellImageView.frame.size
spinner?.stopAnimating()
}
}
}
Upvotes: 0
Views: 184
Reputation: 22343
Just get the image from your imageView from your cell and get the height of your image and use it in the heightForRowAtIndexPath
:
var cellHeight = cell.yourImageView.image?.size.height
Upvotes: 0