【问题标题】:Table View with the cells having fixed height and dynamic height具有固定高度和动态高度的单元格的表格视图
【发布时间】:2020-07-13 10:16:23
【问题描述】:

我有一个包含多个部分的 tableview。在第一部分中,我想要 4 个具有固定高度的表格视图单元格,而在所有其他部分中,我想要具有动态高度的表格视图单元格。 问题是当我填充表格视图时,部分中的动态单元格> 0 单元格没有根据内容调整大小。

对于动态高度,我在 viewDidLoad 视图控制器委托中使用了以下几行。

tableView.estimatedRowHeight = 83
tableView.rowHeight = UITableViewAutomaticDimension

其他方法有:

func numberOfSections(in tableView: UITableView) -> Int {
    return configRowsInSection.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    //configRowsInSection = listingsDetail.Specifications[IndexPath.sec].Configurations
    switch (section) {
       case 0:
          return 4
       default: 
        return Configurations.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.section == 0 {
        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "BannerCell", for: indexPath) as! BannerCell
            cell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
            cell.collectionViewOffset = storedOffsets[indexPath.row] ?? 0
            //addPageControl(cell.collectionView)
            
        }
        else if indexPath.row == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "DetailCell", for: indexPath) as! DetailCell
            cell.Model.text = formatTitle(listingDetail: listingsDetail) 
            cell.SerialNumber.text = "Serial# " + listingsDetail.SerialNumber
        }
        else if indexPath.row == 2{
            let cell = tableView.dequeueReusableCell(withIdentifier: "PriceCell", for: indexPath) as! PriceCell
            cell.Price.text = "PRICE  "+listingsDetail.AskingPrice!
        }
        else if indexPath.row == 3 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "HighlightCell", for: indexPath) as! HighlightCell
            cell.filterText.text = listingsDetail.filter
            cell.seatsText.text = listingsDetail.seats
            cell.wheelsText.text = listingsDetail.wheels
            cell.hoursText.text = listingsDetail.hours
        }
    }
    else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SpecificationCell", for: indexPath) as! SpecificationCell
        cell.configValueBig.text = specificationDetail[indexPath.section-1].Configurations[indexPath.row].Desc
    }
    return UITableViewCell()
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    var rowHeight:CGFloat!
    if indexPath.section == 0 {
        if indexPath.row == 0 {
            rowHeight = 200
        }
        else if indexPath.row == 1 {
            rowHeight = 100
        }
        else if indexPath.row == 2 {
            rowHeight = 70
        }
        else {
            rowHeight = 144
        }
        return rowHeight
    }
    else {
        rowHeight = 83
        //rowHeight = UITableView.automaticDimension (This makes the row height as -1) 
    }
    return rowHeight
}

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return 83
}

处理具有固定高度行的部分和具有可变高度行的其他动态部分的表格视图的最佳方法是什么。 提前致谢!

【问题讨论】:

  • 查看控制台中是否出现NSLayoutConstraint 错误。
  • no @PGDev 没有任何约束被打破。

标签: ios swift uitableview resizable uitableviewautomaticdimension


【解决方案1】:

尝试确保您的第二部分从底部开始具有正确的约束。 处理动态高度的最好方法是使用UITableView.automaticDimension

func numberOfSections(in tableView: UITableView) -> Int {
    return configRowsInSection.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    //configRowsInSection = listingsDetail.Specifications[IndexPath.sec].Configurations
    switch (section) {
       case 0:
          return 4
       default: 
        return Configurations.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.section == 0 {
        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "BannerCell", for: indexPath) as! BannerCell
            cell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
            cell.collectionViewOffset = storedOffsets[indexPath.row] ?? 0
            //addPageControl(cell.collectionView)
            
        }
        else if indexPath.row == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "DetailCell", for: indexPath) as! DetailCell
            cell.Model.text = formatTitle(listingDetail: listingsDetail) 
            cell.SerialNumber.text = "Serial# " + listingsDetail.SerialNumber
        }
        else if indexPath.row == 2{
            let cell = tableView.dequeueReusableCell(withIdentifier: "PriceCell", for: indexPath) as! PriceCell
            cell.Price.text = "PRICE  "+listingsDetail.AskingPrice!
        }
        else if indexPath.row == 3 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "HighlightCell", for: indexPath) as! HighlightCell
            cell.filterText.text = listingsDetail.filter
            cell.seatsText.text = listingsDetail.seats
            cell.wheelsText.text = listingsDetail.wheels
            cell.hoursText.text = listingsDetail.hours
        }
    }
    else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SpecificationCell", for: indexPath) as! SpecificationCell
        cell.configValueBig.text = specificationDetail[indexPath.section-1].Configurations[indexPath.row].Desc
    }
    return UITableViewCell()
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    var rowHeight:CGFloat!
    if indexPath.section == 0 {
        if indexPath.row == 0 {
            rowHeight = 200
        }
        else if indexPath.row == 1 {
            rowHeight = 100
        }
        else if indexPath.row == 2 {
            rowHeight = 70
        }
        else {
            rowHeight = 144
        }
        return rowHeight
    }
   
    return UITableView.automaticDimension
}

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableView.automaticDimension
}

【讨论】:

  • 根据需要为可调整大小的单元格添加约束。没有一个约束被打破。问题是,一旦我将行高设置为 UITableView.automaticDimension,部分 > 0 中的所有单元格都变为空,并且返回的行高为 -1
【解决方案2】:

对于 cellForRowAtIndexPath 中的所有自定义单元格,我们需要返回单元格:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
    if indexPath.row == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "BannerCell", for: indexPath) as! BannerCell
        cell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
        cell.collectionViewOffset = storedOffsets[indexPath.row] ?? 0
        //addPageControl(cell.collectionView)
        return cell
    }
    else if indexPath.row == 1 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "DetailCell", for: indexPath) as! DetailCell
        cell.Model.text = formatTitle(listingDetail: listingsDetail) 
        cell.SerialNumber.text = "Serial# " + listingsDetail.SerialNumber
        return cell
    }
    else if indexPath.row == 2{
        let cell = tableView.dequeueReusableCell(withIdentifier: "PriceCell", for: indexPath) as! PriceCell
        cell.Price.text = "PRICE  "+listingsDetail.AskingPrice!
        return cell
    }
    else if indexPath.row == 3 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "HighlightCell", for: indexPath) as! HighlightCell
        cell.filterText.text = listingsDetail.filter
        cell.seatsText.text = listingsDetail.seats
        cell.wheelsText.text = listingsDetail.wheels
        cell.hoursText.text = listingsDetail.hours
        return cell
    }
}
else {
    let cell = tableView.dequeueReusableCell(withIdentifier: "SpecificationCell", for: indexPath) as! SpecificationCell
    cell.configValueBig.text = specificationDetail[indexPath.section-1].Configurations[indexPath.row].Desc
  return cell
}
return UITableViewCell()

}

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 83

}

【讨论】:

    猜你喜欢
    • 2018-05-06
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    • 2015-09-26
    • 2017-07-19
    • 1970-01-01
    • 2015-04-16
    • 1970-01-01
    相关资源
    最近更新 更多