Табличный вид с ячейками с фиксированной высотой и динамикой c высота - PullRequest
0 голосов
/ 13 июля 2020

У меня есть одно табличное представление с несколькими разделами. В 1-м разделе мне нужны 4 ячейки tableview с фиксированной высотой, тогда как во всех других разделах я хочу ячейки tableview с динамической c высотой. Проблема заключается в том, что когда я заполняю табличное представление, ячейка Dynami c в разделе> 0 ячеек не меняет размер в соответствии с содержимым.

Для Dynami c height я использовал следующие строки в моем 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
}

Как лучше всего обрабатывать табличное представление, имеющее один раздел со строками фиксированной высоты и другими динамическими c секций со строками переменной высоты. Заранее спасибо!

Ответы [ 2 ]

0 голосов
/ 13 июля 2020

Для всех пользовательских ячеек в 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

}

0 голосов
/ 13 июля 2020

Постарайтесь убедиться, что ваш второй раздел имеет правильные ограничения снизу. Лучший способ обработать динамику c высота - использовать 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
}
...