Update UITableViewCell height after asynchronously loading content - uitableview

I'm using a framework to asynchronously load images (Pinterest's PINRemoteImage) in combination with a UITableView. On one of my UITableViewCells I have such an image and I first initialize it with a placeholder and it's then later replaced with the real image. I don't know the real image's dimensions at time of loading and therefore the placeholder's dimensions and the ones of the real image may be different.
Using KVO, I watch the UIImageView's image property and when it's re-set to the real image, I adjust the the view's height constraint to the appropriate size. This directly shows when I first see the image. It has the appropriate size.
The cell's contentView however, does not update it's height to fit around the image (and the other views) properly until the cell get's dequeued a second time. Only then is has the cell the appropriate height.
I assume that I have to tell the table or the cell itself that it needs to update the layout manually. But between all the layout… functions I'm not quite sure which to use where right now.
So, where's the best way to make the cell update it's height to the updated image height? Other answers to similar questions on SO suggest to reload the cell, but that seems a bit too much to me. And I'd have to build a callback from the cell (where the image lives) to the table itself which could then trigger the reload.
Thanks!

Related

How to get this image/text iOS layout from Apple?

I encountered this image on Apple's UI Design Do's and Don'ts:
Of course the UI on the left is the one I am shooting for, my question is what is the best way to go about getting this UI on the left? My initial thought was to use a static table view, with the top cell being the image and the cell below it but I came up with two problems with that approach:
How would I get that little water droplet image to span across both cells?
How do I make sure that the text field below the image is always the right length no matter how much text is put in (i.e. resizing the text field and the cell depending on the amount of text)?
My second thought was to use a UIScrollView and put the image at the top, constrain the water droplet in between them and then contain a text field below it, but then:
How would I make sure that the text field always the right length no matter the amount of text put in?
It is pretty impossible to constrain these items in a UIScrollView, how do I properly constrain this in a UIScrollView?
Why not use a UITableView, and make your cell in the interface builder that can be called through a reuse identifier. And each cell would have the image along with the relevant information that's required. This would fix your first option since you would be able to place the water droplet image over your image and your text.
For your second issue, making sure the cell is of correct height can be done by giving the UITableView dynamic height.
For example, you wouldn't need to insert a heightForRowAtIndexPath function on this tableview, and you can also define:
tableView.estimatedRowHeight = 100; //Can be any value really, but giving a reasonable estimate is helpful
tableView.rowHeight = UITableViewAutomaticDimension;
Also, your the textView you want to use shouldn't be set with any fixed width's or height in the interface builder.
TableView Image . Your imageView would be set up somewhat like the blue section in this picture (but fitting the entire width of the screen instead) and your textView would be like the red one. They would all fit in one cell and would make it much easier to organize than using 2 separate cells for one set of content, and you wouldn't have to get into the hassle of setting up the UIScrollView.

How to get an image size earlier to determine a views size?

Here is a view with a UIImageView in it. I will set different images to it determined by different situation. So what i do is to get the size of image to resize the view. Do we have a better way to determine the size of the UIImageView and not to resize its size?
I face this problem when i have a UITableViewCell in the middle of a UITableView. I show my tableview first without the image. Then after i got the image, i reload the cell to resize the image view. It seems it is not so good in visual effect.

Not resizing downloaded images correctly

I have added a table view, and I am display image in the cells. I have also added this code:
So that the cells resize depending on the image.
When I launch my app though, I get this :
[![enter image description here][1]]
And the images do not load untill I start scrolling...If I scroll down half the page then go back to the top, I get this: Which is correct
[![enter image description here][2]][2]
Any ideas? I have researched on google and tried the odd solution for the older versions of Xcode, But nothing seems to work!
If the image is already downloaded and resident in your cache when you pass the post to the cell them the image view will have an image and therefore an intrinsic content size. This all allows the table view to calculate the required layout.
If the image needs to be downloaded then the image view won't have an image and will size to zero because there is no intrinsic content size other than that.
If your images are all the same size then you can supply a placeholder image while waiting for the download and the cell will size appropriately.
Note also that you aren't updating the cell when the image has downloaded. When you do this you do need to check that the cell hasn't been reused (or you'll be showing the wrong image). If you don't have a placeholder or can't guarantee the image size then you will also need to ask the table view to update.
Maybe your post.downloadImage() and post.fetchLikes() should callback, and in callback, should reload cell in mainThread. hope it help.

Resizing UITableViewCell to fit Content Performance

I am trying to create a table to show posts by users with possibly variable lengths. I want each cell in my table to resize to fit the content of the UILabel containing the text of the post. Currently I am doing this with auto-layout, programmatically setting constraints and calculating the height of the table view cell based on the height of the UILabel. Everything looks as I want it to, except when rapidly scrolling through the table the performance is horrendous, with the CPU getting fully maxed out and unable to keep up with setting the constraints for each cell as they are reused and placed with new text.
I was wondering if anyone knows of a better way to do this. Is there a way I can continue to use auto layout to size my cells without sacrificing performance? Or would the best solution be to create different sized cells with different identifiers, and just choose the best fit based on the UILabel text size.
I also read somewhere that UICollectionView has much better performance in displaying variable-sized content, would it be worthwhile/possible to try configuring a UICollectionView to display the messages, and somehow make it look like a UITableView?
Essentially I just need a suggestion on how to display messages of variable sizes (up to about 7 or 8 lines of text max) in a TableView-like manner without causing massive slowdowns as the user rapidly scrolls through the table.
Thank you
The best trick in the book for things like this is cacheing. Add to your model anything that's expensive to compute (not the views themselves, but the computational results, like view bounds sizes). Make your datasource methods all about looking up and assigning, not at all about computing.
The way I had implemented adding the constraints, the constraints were updated whenever [self updateViewConstraints] was called, regardless of whether or not the cell had already had its constraints calculated and applied. I resolved this by adding a BOOL property to my custom TableViewCell called didUpdateConstraints, and setting it to YES as soon as constraints are updated the first time. Then in my [self updateViewConstraints] method I only update the constraints if !self.didUpdateConstraints
Now that constraints are not being needlessly updated when all views have already been correctly constrained, the performance is significantly better, and I don't observe any slowdown upon scrolling through the table.

how to set the size of the UIImage to be placed into a tableView cell

i have some UIImage's which i am supposed to place in a UITableView. The size of the images is not constant. It can be smaller than the tableview's cell or can even be bigger. This inconsistency in the image size is causing a lot of trouble for my tableView.
Is there any way in which i can adjust the size of the UIImage so that it fits exactly to my tableView cell ???
You can try and rescale you UIImage so that it fits the row height. Have a look a the UIImage+SDSCategory.h category available on GitHub. Many people will tell you that images should not be scaled on iPhone/iPad due to performance reason and that this should be done on the server; when this is an option, it is the best option. But if you have no other option, then also scaling on iPhone/iPad can be fine.
Otherwise you could make your table view row higher, so that the UIImage fits into it naturally. Check the – tableView:heightForRowAtIndexPath: method in your delegate.

Resources