How can I change row height from cellForRowAtIndexPath? - swift

I need to change row height in cellForRowAtIndexPath, using UITableView.
heightForRowAtIndexPath executes before cellForRowAtIndexPath, but in cellForRowAtIndexPath I'm settings content to rows, depended on which height must be recalculated.

You can't, the only place you can and should change is heightForRowAtIndexPath
But there are other solutions for you problem, that are explained very well here
I encourage you to check third solution whitch enable Row Height Estimation.

You can set the height in the viewDidLoad for all cells
self.tableView.rowHeight = 88.0
or use the event heightForRowAt where u have the IndexPath for the cell
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 88.0
}

Related

How to stretch cell to full screen?

I have a UIView inside a table cell. I need to stretch my cell on full screen.
In my table view I have this:
table.rowHeight = UITableView.automaticDimension
But now I can't to make this.
My code is:
containerView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
verticalStackView.snp.makeConstraints { make in
make.center.equalToSuperview()
make.leading.trailing.equalToSuperview().inset(20)
}
I need to get this:
But I have:
So UITableView.automaticDimension will tell the program to find the minimum height this cell can be without any clipping (to simplify it)
What you'll need to do is intercept the cell in
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if self.myData.isEmpty {
return tableView.frame.height
}
return UITableView.automaticDimension
}
Assuming you show that cell when there Is no data to be shown, you can just do a check to see if you data array isEmpty, and if so, make the cell the height of the tableView

How to programmatically adjust the height of a cell in a TableView if everything is created programmatically, without using a storyboard?

I've looked at a lot of examples, but somehow they don't work.
My Code Below.
Objects constraints in a custom cell:
Below is the code in the class:
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
view.addSubview(tableView)
tableView.separatorStyle = .none
tableView.estimatedRowHeight = 200
}
//tableview ->
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 340
}
There a couple of things worth addressing:
1. Do not setup your layout in layoutSubviews()
This method will get triggered multiple times everytime there has been a change to the layout made, meaning all of these constraints will be duplicated, and will eventually cause problems. Try to set them in init of the cell.
2. Do not implement heightForRowAt
When implementing this function:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 340
}
you are telling tableview to give the cell this exact size. According to documentation:
The value returned by this method takes precedence over the value in the rowHeight property.
Remove this code!
3. Set rowHeight to automatic size value
You have already correctly set the estimatedRowHeight value, but we are going to also need the rowHeight property to be set to UITableView.automaticDimension.
tableView.rowHeight = UITableView.automaticDimension
And now you should be good to go!
Bonus: Scrolling Performance improvements
Table view will benefit from any additional information on the size of the cells and you can supply that information with estimatedHeightForRowAt if you are able to calculate a better approximate than the tableView.estimatedRowHeight value you have set in the initial setup.
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
// return more accurate value here
}

how to increase cell height dynamically in swift 4

I am new in swift and I am trying to create a school project. The problem I face is I have multiple cells and I don't know how to increase the cell height. I have searched, but I am not able to understand how to do it.
Thanks for help in advance.
Implement the following table view method:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//return the size of your cell
}
Like if your cell height is 190 then simply return 190 here.

Tableview set row height dynamically based on content in swift?

I am struck in setting tableview cell row height in swift , On scrolling the row height is changing, please check the screen shot.
For the first time its scrolling perfect on scrolling multiple times the height is not proper. I am using too many custom tableview cells
In viewDidLoad
I added this
self.tableView.rowHeight = UITableViewAutomaticDimension
and also added these delegate methods.
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return UITableViewAutomaticDimension
}
Can anyone guide me to achieve this in proper way.

Fill UITableViewCell with UITextView (Static Cells)

If I have a static uitableviewcell with a textview inside it, how am I able to set the height of the uitableviewcell to fit all of the textview content on it?
You can use this method.
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 50
}
You can use an If-else condition to make it what ever height you want as long as you return a number(CGFloat)