简体   繁体   中英

tableview different cells has different heights in swift

在此处输入图像描述

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!

}

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 informationlink or this one, the apple official documentation link2

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM