Swift tableviewcell height stays fixed - swift

My tableviewcell height stays fixed even though I do the following:
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 150
I have one imageview and one label inside the cell and I gave constraints to each side.
Also, I tried:
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
I need a fixed height for my cell. It doesnt need to be dynamic. But no matter what i tried the height stays around 50px.
Any help?

Setting automaticDimension is based on using auto layout in your cells.
This means you must add a NSLayoutConstraint to all sides of a UILabel (or any other control you use in your cells) and the UITableViewCell container in interface designer or in code. In case of a label, configure setting 'Lines' to 0 and 'Line Break' to Word Wrap.

you can set the height you want with heightForRowAtIndexPath, hopefully that helps

Related

How to set constraints for Xib for dynamic height of a cell...?

I am using xib to display the data but if the data for labels are empty am hiding the labels but height of the cell is not decreasing based on the contents.Thanks in advance your suggestions are helpful
You should use auto layout constarits,
not set a fixed height for labels please connect to top and bottom
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}

Setting the height: TableViewCells with custom Nib that are different sizes

I have a TableViewController that has a custom TableViewCell under the identifier "customCell". Heres an image of the configuration of the cell along with the IBOulets connected to it:
The cell takes information from my backend and presents it. The description text view (just noticed that I accidentally named it descriptionLabel) doesn't allow scrolling so it expands based off of the content that it's holding. The database is being processed correctly from the database, and it's displaying on the app. The only problem is that the cell is not its correct height. On the TableViewControl that's registering this cell through its identifier, I automatically set the height of the cell using UITableViewAutomaticDimension in heightForRow:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
but that's not working either. *The only thing that works is when I set the height of each cell to a standard value such as 200. That doesn't work for me because each cell will be a different height because of the length of the textView.
How do I set the height of a custom nib (tableViewCell) so that it adjusts based off of the content within it instead of setting the height to a specific value?
1-Remove both textViews and replace them with labels
2- Title lbl with theses constraints
top,leading,trailing , .lines = 0
3- Description lbl with theses constraints
bottom ,leading,trailing to contentView,top to bottom of title lbl , .lines = 0
Off course you can leave the 2 textviews , but you have to give each one an initial height and do this in
override func layoutSubviews() {
super.layoutSubviews()
self.titleTvHeight.constant = self.titleTv.contentSize.height
self.desTVheight.constant = self.desTv.contentSize.height
}
//
Don't forget to set this in viewDidLoad of the VC
tableView.estimatedRowHeight = 200
tableView.rowHeight = UITableViewAutomaticDimension
Anyway you can remove heightForRowAt

in UITableviewcell in Xcode 9.0.1 cell size it not expanding at runtime?

I have tried with drag and drop UITableviewcell and change row height from file inspector cannot expanded cell height at runtime.
in Xcode 9.0.1 I am facing issue regarding Cell height not increasing at run time.
Swift 3+
If you want dynamic row height, you can define your constraints (making sure they're unambiguous), set the label's numberOfLines to zero, and then in viewDidLoad, tell it that rows should automatically adjust their height:
override func viewDidLoad() {
super.viewDidLoad()
tableView.estimatedRowHeight = your height here
tableView.rowHeight = UITableViewAutomaticDimension
}
OR Try this.
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
You have to configure your cells to be self-sizing:
1: Add the following lines to viewDidLoad(). Specify a tableViewRowHieght that you think will be the average height of the cells displayd in your tableView:
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 200
2: Makes sure all of the labels in your cell are capable of expanding to as many lines as needed. This is done by setting Number Of Lines in Attribute Inspector to 0 as I have done for my Title label in the image below:
3: Finally, you must set constraints for every element in your cell so that the compiler can correctly calculate the size of the cell at runtime. Here I've added two lables contrained the height of the first to the top of the view, and bottom to label 2 below. Then label 2 must be contrained to the bottom of the view. As long as your contraints contain no "gaps" between the top of the cell to the bottom, the compiler can calculate the height of the cell:

Swift, Auto Resize Custom Table View Cells

In my app, I have a table view with an image, label and text view in each cell. I would like to be able to auto-resize the cells depending on the amount of content in the text view. (The text view is the lower most text.)
So far, I have added the correct constraints; leading, trailing, top and bottom to the text view and have disabled scrolling and editing.
In my tableViewController.swift file, I have written this code:
override func viewWillAppear(_ animated: Bool) {
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension
}
However, this is not working as when I add more text to the text view, it just cuts off.
Maybe this has something to do with the card like look, I have got a UIView in each cell acting as a card.
I honestly don't know what I am doing wrong
A picture is below of what my app looks like and if anyone could help that would be greatly appreciated
Check if your constraints are like this(based on your image) :
imageView : set to Top and Leading of your container, with fix height and width values.
label : you can set it to top and horizontal space of your image, with fix height and width as well.
textView : leading to image, top space to label, trailing and bottom to container.
And keep using
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension
in your viewWillAppear()
Update for swift 4.2
Use:
UITableView.automaticDimension
Instead of:
UITableViewAutomaticDimension
Make sure that the content mode is set to Scale To Fill of your UITextView
Make sure that there are no height constraints for the UITextView and the card UIView
Try to add the estimated height into viewDidLoad:
override func viewDidLoad() {
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension
}
Maybe the AutoHeight is not working because of the UIView above the UITextView. Try to call the sizeToFit and layoutIfNeeded methods for the UIView in the cellForRowAt:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Identifier", for: indexPath) as! CustomTableViewCell
cell.vwCard.sizeToFit()
cell.vwCard.layoutIfNeeded()
return cell
}
You can also try the sizeToFit and layoutIfNeeded as well for the UITextView.
Hope this works........
Set bottom of your textView with bottom of that white UIView and make sure that white UIView has left,right,top and bottom constraints :)
Same type of example is explained here programmatically....

How to fill table cell with an image in Swift

I am replicating the effect in the image below. I created a custom UITableViewCell class with an UIImageView property. I set the prototype cell to my custom class. However, I cannot get the table cell's height to resize to the height of the image. There is always space above and below the image.
You can make the heigh of your UITableViewCell dynamic regarding the height of your image. The UITableViewDelegate has a function entitled tableView:estimatedHeightForRowAtIndexPath: that according to Apple:
Asks the delegate for the estimated height of a row in a specified location.
Then you can in your method ask for the height of the UIImageView and set the height for the row properly, see the following example:
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return self.getEstimatedHeightForImageAtIndex(indexPath.row)
}
func getEstimatedHeightForImageAtIndex(row: Int) -> CGFloat {
// here you need to get the height of image for the selected row.
}
There is a nice tutorial you can read to know more about the topic :
UITableView Tutorial: Dynamic Table View Cell Height
I hope this help you.
What I did was to create a custom UITableViewCell, added an UIImageView to the cell and resized it in the storyboard to fill the whole cell.
If you do that though, make sure you add the constraints to have it horizontally and vertically centred, and add the spacing constraints, but change the spacing not to margin (uncheck the box).
See the image below:
And another thing to remember is the content mode of the imageview. I learned a lot from #EmptyStack and #erkanyildiz answers to this question.
I hope you found any of this useful, and I am sorry if none of it helped! :)
Create a custom UITableViewCell, set cell style to custom in storyboard. Add an UIImageView to the cell and resize image to fill the whole cell. Add image outlet to your custom UITableViewCell and use that outlet when seting an image in your TableViewController.
In your viewDidLoad() function add
tableView.rowHeight = UITableViewAutomaticDimension
Then you need your image aspect ratio (width / height) so you can use it when you set your row height.
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath
indexPath: NSIndexPath) -> CGFloat {
return view.frame.width / CGFloat(aspectRatio)
}