How to set automatic height in heighForRowAt indexPath? - swift

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.

Related

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 make Custom Xib Cell Resize Correctly

I'm trying to make a custom xib cell resize based on really how much information the user enters.
I have tried setting the constraints of the xib cell, like this Constraints
I only have top and bottom constraints on the Label and the Date.
In the viewDidLoad() of the viewcontroller containing the cell and tableview i have
self.tableView.rowHeight = UITableView.automaticDimension
and I also have set
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 250
}
GIF of Resize Not Working

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.

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
}
}

swift tableview image and label

I can't seem to figure how to have a tableview that contains a subject, body and image so that if there is not an image to have the subject and body together. see imageauto lay with table prototype
I would like the subject and body together when there is an image in the database and then if there is an image, then subject image body
pin the labels to the top and bottom of the cell. When you don't have an image you set the imageView.isHidden = true. You also return the regular cell height minus the imageView.frame.size.height in UITableViewDelegate.tableview(_:heightForRowAt:).
If you don't like approach just make two different tableViewCells and deque the one with the image when you have an image and the one without the imageView when you don't have an image.
You can do this in your dataSource methods for the tableview. Something like this should work:
//Model:
var model = [Stuff]
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myprototype", for: indexPath)
cell.subjectLabel.text = model[indexPath.row].subject
cell.bodyLabel.text = model[indexPath.row].body
//assuming the image is an optional
if let image = model[indexPath.row].image {
cell.imageView.image = image
}
return cell
}
You might have to set the autolayout constraints for the imageView to have a low compression resistance.
You might also want to make the cells resize by implementing the following in the UITableViewDelegate methods:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}