Fixing Height of TableViewCell - ios

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 :)

Related

How to give size to table view to XIB

I am new to iOS practicing XIB I have created an XIB for a cell
Now When I resize it from here I automatically change its behaviour in tableview I want to give this XIB with as 100% of tableview. I actually want to set Width of this XIB as tableview
You will need to set the size in the UITableViewDelegate's heightForRowAt(_:) method.
For purposes of building UI in interface builder, you can just drag the corner into the size you want.
If you have your constraints setup correctly, the size you use to layout your cell in your XIB has no effect on the resulting use in a table view.
For example, with this layout:
I can stretch the frame like this:
or this:
or this:
or this:
and it will ALWAYS look like this when I run the app:
Try
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return tableView.frame.height
}

Changing UITableView Cell's Height by using storyboard

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
}

TableViewCell not displays the height I set as a constraint

I managed to get this work in the past but now I cannot remember how I did it. I have a UITableView with a dynamic cell and in it, I have an UIView with a height of 174 as a constraint. However, after running the app, it displays the UIView with less height than I defined and also I am getting this error: Warning once only: Detected a case where constraints ambiguously suggest a height of zero for tableviewcell cell's. We're considering the collapse unintentional and using standard height instead:
Those are the constraints I have in the UIView:
I am not able to find a fix for this.
Use this datasource
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 178
}

How to resize custom cells in a TableView?

I am learning iOS development and Swift. I am trying to create a simple to-do app, with custom cells in a TableView. I have created the custom cell with a XIB file. In this, the cell looks like this:
However, when I run the app on the simulator, it looks like this:
Where am I going wrong and how can I Fix it?
You can fix the cell height to a default value by putting something like
self.tableView.rowHeight = 44.0
In your viewDidLoad, where tableView corresponds to the IBOutlet that is your UITableView in your xib file.
If you would like to do this dynamically e.g. by calculating the size of the cell's contents and then resizing the cell so it fits nice and snug, you should implement
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 44 // The cell's height
}
Be sure to set your UITableView's delegate to self to use this last method.
Please see this tutorial for more info.

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.

Resources