Reputation: 73
1. I have a table view with two cells. I want these two cell’s height automatic dimension. In the first cell, a label is there, according to the label the cell height will increase. And in the second cell, a collection view is there, which contains user photos. As the numbers of photo increase, the collection view height will increase and according to that the table view cell size will increase.
2. I have the following code but I am not getting the desired result. In my code, I have two table view cells "CustomCell" and "image cell". The image cell has a collection view. Is it a good practice to take a collection view inside a table view cell or should I use a scroll view here? But as the number of photo increase, the collection view height will increase.
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0{
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as? CustomCell
cell?.personName.text = "New User"
cell?.aboutPerson.text = "Near the beginning of his career"
return cell!
}
else{
let cell = tableView.dequeueReusableCell(withIdentifier: "imageCell", for: indexPath) as? imageCell
return cell!
}
return UITableViewCell()
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
var height:CGFloat = CGFloat()
if indexPath.row == 0 {
return UITableView.automaticDimension
}
else {
let indexPath = IndexPath(row: 0, section: 0)
let cell = tableView.cellForRow(at: indexPath) as? imageCell
return (cell?.personPhotoCollectionView.frame.size.height)!
}
return height
}
}
class imageCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 50
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = personPhotoCollectionView.dequeueReusableCell(withReuseIdentifier: "imageCollectionViewCell", for: indexPath) as? imageCollectionViewCell
cell?.personImage.image = UIImage(named: "azamat-zhanisov-om_aSsajsLw-unsplash.jpg")
return cell!
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let numberOfCollumns: CGFloat = 3
let width = collectionView.frame.size.width
let xInsets: CGFloat = 10
let cellaSpacing: CGFloat = 5
return CGSize(width: (width / numberOfCollumns) - (xInsets + cellaSpacing), height: (width / numberOfCollumns) - (xInsets + cellaSpacing))
}
@IBOutlet weak var personPhotoCollectionView: UICollectionView!
}
Upvotes: 1
Views: 1397
Reputation: 73
By adding the following code, it solved my issue.
override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
personPhotoCollectionView.layoutIfNeeded()
personPhotoCollectionView.frame = CGRect(x: 0, y: 0, width: targetSize.width , height: 1)
return personPhotoCollectionView.collectionViewLayout.collectionViewContentSize
}
Upvotes: 0
Reputation: 196
you should add two methods to your table view controller, heightForRowAt and estimatedHeightForRowAt
try this
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == 0 {
return UITableView.automaticDimension
} else {
return 40
}
}
override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == 0 {
return UITableView.automaticDimension
} else {
return 40
}
}
or mabybe try with this line
self.tableView.rowHeight = 44.0
check this link for a little more information link or this one, the apple official documentation link2
Upvotes: 1