How to make Custom Xib Cell Resize Correctly - swift

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

Related

UICollectionView inside a UITableViewCell — dynamic height calculate isn't correct

I have a dynamic height collection view in table view cell. I used the approach #Igor in this topic.
UICollectionView inside a UITableViewCell -- dynamic height?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DraftCustomerTableViewCell", for: indexPath) as! DraftCustomerTableViewCell
cell.data = notificationDraftListViewData[indexPath.section]
cell.frame = tableView.bounds
cell.collectionView.reloadData()
cell.collectionViewHeightConstraint.constant = cell.collectionView.collectionViewLayout.collectionViewContentSize.height
cell.layoutIfNeeded()
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
dict[indexPath] = cell.frame.size.height
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return dict[indexPath] ?? CGFloat(450)
}
The issue is when I expand the tableview section first time, the cell's height is not calculate right. After expanding first time, the collectionView's height can been calculated correctly. I try to let my data[0].isExpand is true without taping the button and find the table view's cell height is correct. Please see the demo. Is there some approach can avoid this issue? thanks.
Demo: https://youtu.be/YJCJChCU0wQ

How to fix custom UITableViewCell dynamic height?

I'm trying to dynamically calculate the height of the custom UITableViewCell, which looks like this:
https://i.imgur.com/7U6aWgZ.png "UITableViewCell"
So the text "Hi! Could.." can be any size.
I do this in the following way:
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 150
But it doesn't help, it gives me this result:
https://i.imgur.com/c65xMMP.jpg
try giving constraints to all 4 sides of the 'Hi! Could...' UILabel and implement these UITableviewDelegate methods:-
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 150
}
and make sure you have implemented delegates and datasources correctly.

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
}

Custom tableview cell image stretched

first post on this website so excuse any faults.
im having trouble with my image in custom cells in tableview, the image is set to width 345, height: 200 and 200 away from border and cellsize is 400 width. But when it launches everything is fine except image is displayed at about 1000 width instead of 200.
the image content mode is set to scale to fill, clips to bounds. but have tried aspect fit, and same problem.
extension SearchViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier:"searchCell", for: indexPath)
return cell
}

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
}