Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift, custom UITableViewCell with UIImageView. Need to resize cell to image height

I'am trying to make app, that will show images in table view. I have custom cell with image view. It only must download image data from url:

@IBOutlet weak var tweetImage: UIImageView!
var imageData : MediaItem? { didSet { updateUI() }}

func updateUI(){
    tweetImage.image = nil
    if let url = imageData?.url {
        if let data = NSData(contentsOfURL: url) {
            tweetImage.image = UIImage(data: data)
        }
    }
}

I need to get the cell height was changed after the download. It must be equal to the height of the image. I set auto dimension in viewController:

override func viewDidLoad() {
        super.viewDidLoad()
        tableView.estimatedRowHeight = tableView.rowHeight
        tableView.rowHeight = UITableViewAutomaticDimension
    }

I set "aspect fit" to image, I get strange results. The image extends beyond the boundaries of the cell. Maybe i need to set some constraints... I don't know.

Results:

result of it

But i need this:

nice result

like image 486
mr_ivan777 Avatar asked Aug 04 '15 09:08

mr_ivan777


3 Answers

If you want to load images async:

After you load and set new UIImage, you can reload specific cell via UITableView function:

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

In your UIViewController:

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.estimatedRowHeight = tableView.rowHeight
    tableView.rowHeight = UITableViewAutomaticDimension
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "imageDidLoadNotification:", name:"CellDidLoadImageDidLoadNotification", object: nil)
}

func imageDidLoadNotification(notification: NSNotification) {
    if  let cell = notification.object as? UITableViewCell
        let indexPath = tableView.indexPathForCell(cell) {
        tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
    }
}

In your UITableViewCell

func updateUI(){
    tweetImage.image = nil
    if let url = imageData?.url {
        if let data = NSData(contentsOfURL: url) {
            tweetImage.image = UIImage(data: data)
            NSNotificationCenter.defaultCenter().postNotificationName("CellDidLoadImageDidLoadNotification", object: self)
        }
    }
}
like image 189
Vitalii Gozhenko Avatar answered Oct 17 '22 09:10

Vitalii Gozhenko


If you want to use dinamic table with UITableViewAutomaticDimension, then you need to set your constraint properly (image). (for correct width and height, I suggest you to use aspect ratio)

With static table view, you should implement optional func tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat, and count the cell size manually.

like image 31
Peter Avatar answered Oct 17 '22 10:10

Peter


Of course, you need to set constraints for your image in the cell

screenshot

Maybe this screenshot will help you

like image 29
Tikhonov Aleksandr Avatar answered Oct 17 '22 08:10

Tikhonov Aleksandr