tableView cell dynamic height only works properly after some scrolling - ios

I'm trying to set the height of my tableview cell based on the height of an image inside my cell, the cell is rendering properly if I scroll a little up and down otherwise it is not getting updated, I have set all necessary constraint the image view (leading, trailing, top, bottom).
and i'm setting the image in my cellforrowat
I have set rowHeight as UITableViewAutomaticDimension
I have tried these solution
1. aspect constraint
2. dynamic cell height scrolling issues solutions

make sure you have set top-bottom constraints in storyboard.
save the thumbnail image as data to local storage (I'm using core data)
using the thumb image, calculate the aspect of the image
customise the row height as you want in heightForRowAt method.
I have answer this here:https://stackoverflow.com/a/61768782/7514688

Related

Xcode image occupying whole tableView instead of cell

So I am creating a prototype which I will use later.
I have a table view with one cell, then I will make the whole table populate several cells (that is working)
My problem is, the image on my cell, instead of occupying the cell to occupies the whole table view as follows
Any ideas?
Thanks
Storyboard
Simulator
EDIT
repo: https://bitbucket.org/eduardoreecreate/coderswag-ios
Try setting the image view's content type to AspectFit and ClipToBounds to true
By default, an image view under the influence of auto layout wants to be the size of the image. Your image is big. Therefore the image view is big. Therefore the cell itself is big, because you are autosizing the cell height to match the height of its contents.
The simple solution is: don't do that! Before you put the image into the cell, munge it in code so that it is the correct height. It is very foolish to put a huge image into a small table cell in any case, or any small image view, because you're still wasting all that memory. Always try to crop / shrink the image before putting it into the interface.
More elaborately, you can give the image view an absolute height constraint to keep it from growing beyond a certain height. Or, as you've already been told, don't use automatic cell height; set the cell height to some absolute value by implementing heightForRow.
Implement heightForRowAt UITableView's delegate
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 150
// Customize or write a logic as per your requirement like for 1/4 of screen UIScreen.main.bounds.size.height / 4
}
If you select the cell in IB, then the Size Inspector shows Row Height at the top (with a "custom" checkbox), but if you select the whole table view the Size Inspector shows Row Height at the top there too (no "custom" in this case). The table view setting is used for dynamic cells. (Not sure if it's intentional)

UITableView content size not getting proper

I have UITableView for display for chat message list. I am using 5 type of different with dynamic height. Minimum cell height from 20 to maxHeight( approx 1000 px). UITable view cell dynamic height is set by "self.messageTableView.rowHeight = UITableViewAutomaticDimension".
What Should be common estimate row height.
After reload UITableView content size not getting properly. If user scroll then content size increased.
It is not enough to just set rowHeight to automatic dimension. You also need to install proper constraints for cell subviews so the cell can calculate its height based on that constraints.
To use UITableViewAutomaticDimension for dynamic height calculation, ensure
1 - Leading, Trailing, Top and Bottom constraints to cell are added correctly and not creating any conflicts
2 - Add cell.layoutIfNeeded() in cellForRowAt method of UITableViewDelegate
3 - You can give any height as estimatedRowHeight, 44 is the default value, but you can give 20(your minimum) as well.

UITableViewCell Not Sizing To UIImageView

I was wondering if anyone knows hot to fix this issue. When I run the app, the image gets cut off instead of displayed. I have attached photos to show in detail.
Since you have two kind of Cells in the Table View, you have to set the height of both cells programatically inside heightForRowAtIndexPath.
Currently, you have only one cell size and I think it is default to 44.0.
May be you doesn't set imageview's constraints properly.
Set top, bottom, left, rignt constraint of imageview with tableviewcell frame properly. Give a aspect ratio size of the imageview.
And return UITableViewAutomaticDimension from heightForRowAtIndexPath and estimatedHeightForRowAtIndexPath delegate functions.
This will work !!

UITableViewCell sizing with optional image (Swift)

I am having a doozy of a time trying to figure this one out:
I have a UITableViewCell with a fixed sized UIImage (250 x 250) pinned to the top of the cell's View and a UILabel of variable text size underneath with its top pinned to the bottom of the UIImage and its bottom to the bottom of the cell's View.
I have a use case where an image is not always guaranteed and so the image view purposely collapses to 0 height so it does not break autolayout on the UILabel's top pinning.
I assumed that this would allow the UITableViewCell to shrink down to the size of the label but instead, it stays the default height and autolayout stretches the label's height (to maintain the pinning to the top and the bottom) with the text centered in the middle.
I used all the usual suspects, UITAbleViewAutomaticDimension and estimatedRowHeight blah blah (what you would probably suggest first!) and it was to no avail.
What am I doing wrong?
I did see something about changing the Priority for the constraints in IB to 250 instead of 1000 so they could break. And also some references to resizing the UITableViewCell manually (lame and nasty) by calling requiredHeight() on the UILabel which broke the layout, so what am I doing wrong here? Thanks!
I have a use case where an image is not always guaranteed
For this situation you can create 2 type of UITableViewCell: the first one with UIImageView and the second without it. Then, in cellForRowAtIndexPath init the cell you want.

Why is contentView height of UITableViewCell always one point smaller than the UITableViewCell?

My cells, at the UITableViewCell level, have a height of 44 points. The contentView says 43 points, and it's greyed out in IB. How can I make the content the same size as the cell?
Note that I'm designing my cells in separate xib files.
I want my cells to have no margin between each other.
I have also set None as separator style of the table view.
Row height in IB is also set to 44 points. Not using any sections.
If you're using storyboards setting the Separator of the tableView to None (instead of Default) gets rid of the 1pt separator and the contentView's height becomes equal to the cell's height (even though it's grayed out)
Note: This solution doesn't work with xib files (at least doesn't update the IB, didn't check at run-time).
I figured it out. I was using full sized background images as background for the cells. As soon as I stopped putting separate UIImageView:s as background in the contentView, and instead assigned the background UIImageView:s to the cell's backgroundView property, and then set the height of the cell as well as the height of each cell in the uitableview (and also set the contentView background color to fully transparent!), things started working as I wanted them to. The contentView height will still be one point smaller, but now the cells align perfectly to each other. For this I don't need to have the contentView height match the UITableViewCell height.

Resources