how to increase cell height dynamically in swift 4 - swift

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.

Related

How to set automatic height in heighForRowAt indexPath?

I am tried counting the number to make UIImage perfect size each time, so I am making the comic app, and I can upload without a set number to make perfect like that.
I try to import IMG in which UIImage from comicsCell (UITableViewCell) by the Storyboard used.
here i am trying to research for almost 2 weeks and nothing to find a trick way to automatically UIImage Height.
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return Int(comicsCells.IMG.bounds.height)
// error: Cannot find 'comicsCells' in scope
}
Let me know if you find any code to allow me to import the image with automatic height itself.
Thank you!
You can try the automaticDimension like that:
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
PS: Top and bottom constraints is required for this to work.
Check this post.

Swift Changing the size of tableview cells

just like the title says I'm searching for a way to change the size of each cell in my tableview. I have 2 questions:
1) How can I change the size of all cells by the same amount in my tableview?
2) Is it possible to automatically adjust the size of the cell based on the length of the text in that particular cell?
I'm aware that there are already similar questions asked before but I couldn't manage to implement their answers correctly as they've been outdated for some time now. I'd appreciate any help, thank you in advance!
1) To change size of all cells with same type, first you need to create CellType, here is the explanation how to do this.
https://stackoverflow.com/a/51979506/8417137
or you can check for indexPath.row, but it's not cool :)
Than here:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
switch cellType {
case firstType:
return firstCellTypeHeight
case secondType:
return secondCellTypeHeight
default:
return defaultCellHeight
}
}
2) Yes, its possible. You create your CustomCell. In your CustomCell you should
setup your textFields or labels inside ContentView. Your views with text will stretch your cell.
Important thing. In method above you must return special height like that.
return UITableViewAutomaticDimension
For example your code will looks like that.
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
switch cellType {
case firstType:
return firstCellTypeHeight
case secondType:
return secondCellTypeHeight
// Your stretching cell
case textCellType:
return UITableViewAutomaticDimension
}
}
override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
switch cellType {
case firstType:
return firstCellTypeHeight
case secondType:
return secondCellTypeHeight
// Your stretching cell
case textCellType:
return UITableViewAutomaticDimension
}
}

Tableview creates five rows on one row?

I have a tableView with a custom cell that I have told should give me 5 cells in return of this custom cell. The question now is that I am running the app and getting five rows on one row. I have changed from default size of cell to custom but that is nearly the only thing I have done. So now I wonder what can create this kind of problem?
The code of my tableView looks like this.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "theoryCell") as! theoryTVC
return cell
}
So the problem is you are using custom cell with custom height but you are not setting height for row in table view method.
Try this:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return 100.0 //Choose your custom row height
}

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.

How can I change row height from cellForRowAtIndexPath?

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
}