【问题标题】:tableview different cells has different heights in swifttableview不同的单元格在swift中有不同的高度
【发布时间】:2019-11-17 04:25:56
【问题描述】:

1.我有一个包含两个单元格的表格视图。我想要这两个单元格的高度自动尺寸。在第一个单元格中,有一个标签,根据标签,单元格的高度会增加。在第二个单元格中,有一个集合视图,其中包含用户照片。随着照片数量的增加,collection view 的高度会随之增加,table view cell 的大小也会随之增加。

2。我有以下代码,但没有得到想要的结果。在我的代码中,我有两个表格视图单元格“CustomCell”和“图像单元格”。图像单元格有一个集合视图。在表格视图单元格中获取集合视图是一种好习惯,还是应该在这里使用滚动视图?但是随着照片数量的增加,collection view 的高度也会增加。

    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!

}

【问题讨论】:

    标签: ios swift uitableview uicollectionview


    【解决方案1】:

    您应该在表格视图控制器中添加两个方法,heightForRowAt 和estimatedHeightForRowAt

    试试这个

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

    或者也许试试这条线

    self.tableView.rowHeight = 44.0
    

    查看此链接以获取更多信息link 还是这个,苹果官方文档link2

    【讨论】:

    • 嗨,卡米洛,感谢您的回复。我写了这段代码。但我仍然遇到同样的错误。是否有任何自动布局问题?我在我的问题中添加了一张图片,请查看。
    【解决方案2】:

    通过添加以下代码,它解决了我的问题。

     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
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多