Changing UITableView Cell's Height by using storyboard - ios

I'm using Swift5 and Xcode 11.4.1 on macOS Catalina.
main.storyboard
As you can see here, I selected tableview cell, went to the size inspector and changed the height as 100 and hit enter.
It is successfully changed as 100 height on storyboard but when I run this thing,
Simulator screenshot
It looks like this, the image link above.
The height is set as 44 not 100.

Add this method to the class that conform's to your UITableView's delegate to change the tableView's height.
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}

Related

swift tableView.contentSize.height different iOS 13 and iOS 11 , why?

my code is
paymentTableviewHeight.constant = paymentTableView.contentSize.height
but different result iOS versions. It works correctly when I try it on ios 11 but iOS 13 is shorter.
I have fixed this problem by using below code:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
tableView.layoutIfNeeded()
self.tblViewHtConst.constant = self.tableView.contentSize.height
}
Calling tableView.layoutIfNeeded() UITableView will calculate contentSize again and then you can set it to height constraint.
If layoutIfNeeded() is not called before setting height constraint, it is picking up value which is set in estimatedRowHeight, in my case it was large then actual content size and blank space was visible.

Fixing Height of TableViewCell

I am trying to create an homepage like instagram for iPhone, in the storyboard I have set the 'post cell' using a UITableViewCell and anchoring it using AutoLayout. The cell in the storyboard looks like this:
As you can see the height is fixed to 555 pixel but when I start the simulator the cells of the UITableView don't have the height I fixed in the storyboard size inspector. It looks like this:
How can I fix this issue?
If you want to set fix height then you can set in controller also.
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 555
}
It may be Help you :)

Dynamic tableViewCell height

I have a tableViewCell with a label inside that could be multiple lines tall. I've set the label's Lines property to 0. However, when I make the label's text have multiple lines the text gets cut off. Here's how I've set up my storyboard:
Does anybody know how I made the table's cells just tall enough to contain the labels within?
Setting Dynamic Cell height procedure
Pin the label from top and bottom. Please refer following Screen shot
Set numbers of line to 0 of the label as from property inspector of xcode, it can be done from code too please refer following screen shot
Implement delegates of table view mentioned below
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 50 // also UITableViewAutomaticDimension can be used
}
You are missing the bottom constraint from the label to the table view cell (as far as I can tell). In order to make autolayout know how large the height of the cell has to be, you need to supply those constraints.
In addition do not forget to provide the estimatedRowHeight to the table view. If that value is not given, automatic cell sizing will not work.

Dynamic UITableViewCell not resizing according to the content

Senario A:
If I set the label content in cellForRowAtIndexPath, the cell correctly get resized.
Senario B:
If I change the text content in custom action in cell, the cell sized does not get changed.(I do call setNeedsLayout + layoutIfNeeded)
How to fix this?
EDIT:
1) I have set,
myTableView.estimatedRowHeight = 71.0
myTableView.rowHeight = UITableViewAutomaticDimension
2) I have correctly added auto layout constraints.
I was running into this issue, and my problem was that I was constraining the content to self (the UITableViewCell) and not to self.contentView (the contentView OF the cell). Hope this helps someone else who has built their cells all in code!
In my case, the cell's custom size was enabled:
After you change the text of the cell, just reload that particular cell or simply call mainTableView.reloadData().
To reload that cell-
//indexPath is indexPath of cell you just changed label of
mainTableView.reloadRows(at: indexPath, with: .automatic)
In my case, in the same cell I had an imageView in the top left corner with a "center vertically in container" constraint, and a "top space container" constraint.
Obviously to satisfy this two constraint the cell must have an height equal to:
(height of the imageView / 2) + (length of the top space container constraint).
This height is not enough to fit the label text, so my label had only 1 line visible.
After I have deleted the imageView top constraint all went to the right place, in my case i wanted the image to be centered, if the image had to stay in the top left corner I had to take off the "center vertically in container" constraint.
I hope this can help someone.
First of all, I don't specifically know what was your action on UITableViewCell. So, I assume I do that in UITableViewCell selection.
The below answer only work on iOS 9 and above
But, for some reason, it failed to do it in iOS 8 until it scroll. So, I will update the answer for iOS 8.
I have seen you have used UITableView's estimatedRowHeight and rowHeight at your project. So,
Please check the following
Make sure UITableView's estimatedRowHeight and rowHeight include inside viewDidLoad()
Make sure your UILabel lines set to 0
Make sure there is no constraints about height for your UILabel and let the constraints be like that :
If there are other component also included, make sure only bottom and top space constraints included or top space to container margin and bottom space to container margin.
Every time that you want to update the cell, you have to reload tableView no matter what your current situation will be.
So, don't say anything yet before you try this sample project, #Rikh answer still work. May be you are going in wrong direction. Here's the solution. Please do as I said steps by steps and let me know if that didn't work out. You might need to share your sample project which is causing.
Sample Demo - DynamicCellDemo
UPDATE for iOS 8 : update the following code for iOS 8 users
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if #available(iOS 9, *) {
// do nothing
} else {
tblDynamic.reloadData()
}
}
what you can do is set the AutoLayout constraints for the label in present in the cell from all the sides that is from Top, Bottom, Leading and Trailing. Then add the following UITableViewDelegate method to your class.
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 500
}
This will do the job. As now content in table view cell automatically adjusts the height of the cell.
Try this if it works:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Add the following in your viewDidLoad()
-(void)viewDidLoad{
[super viewDidLoad];
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 140
}

Auto-sizing a uitableviewcell - SWIFT

I am trying to resize a tableviewcell based on the size of a UILabel that can be one or more lines. I need autolayout turned ON for the rest of the app, so I don't want to turn it off to get this working. Target is iOS 7 and iOS 8.
I have tried several solutions, but for some reason I can't seem to get the correct height of the label to automatically adjust or to adjust the cell height.
The problem is occurring in this method: calculateHeightForConfiguredSizingCell. I have put a sample project on Git, so you can see what I am seeing.
https://github.com/mdaymond/cellResizer
This example is based on this article: http://www.raywenderlich.com/73602/dynamic-table-view-cell-height-auto-layout
Update
I checked in an update to the code. It's ALMOST working the way I want and calculating programatically, but for some reason the label height isn't quite sized correctly - it's not getting the full height required. Problem with the original code was that the label needed an explicit width.
Here we go, if you are supporting IOS 7 then you need to implement UITableViewDelegate protocol in your class and then override:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
}
for more information about Dynamic custom UITableViewCell's height based on label text length (check this out) and you can place the code in the heightForRowAtIndexPath function:
height based on label text length
Also override :
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
// example to return the estimated height
return 280
}
Note : by supporting IOS 7 you should handle it manually. there is no such easier way as IOS 8.
But if you are only supporting IOS 8 and later then you can do it simply in the following two lines :
self.tableView.estimatedRowHeight = 280
self.tableView.rowHeight = UITableViewAutomaticDimension
Actually i have download your source code and doing following changes. and UILabel is now support multiline.
Step 1 : Removed programmatically calculate row height. that means commented heightForRowAtIndexPath in your demo.
Step 2: Set following layout constraints on UILabel in your UITableViewCell.
Step 3 : Set number of line to 0 in your xib.
Output :
Here is my code.
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
self.scoutPropertyTable.rowHeight = UITableViewAutomaticDimension + 35
return self.scoutPropertyTable.rowHeight
}

Resources