如何通过更改单元格的高度来创建动态 TableView 页脚

How to create dynamic TableView footer by changing height of cells

我想创建一个与表格视图单元格高度动态相关的页脚。

我的初始情况如下:

如果我单击一行,它会将此单元格的高度更改为 194(之前为 44)

它没有显示所有行。

如果我得到页脚 -150,它看起来像:

如果我关闭所有单元格,它看起来像我用 -150 到达页脚的 150 在这里是白色的:

我的代码:

var selectedCellIndexPath: Int?
let selectedCellHeight: CGFloat = 194.0
let unselectedCellHeight: CGFloat = 44.0
var cellsHeight = [44, 44, 44]

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.beginUpdates()
    if selectedCellIndexPath != nil && selectedCellIndexPath == indexPath.row {
        selectedCellIndexPath = nil
    }
    else {
        selectedCellIndexPath = indexPath.row
    }
    if selectedCellIndexPath != nil {
        tableView.scrollToRow(at: indexPath, at: .none, animated: true)
    }
    tableView.endUpdates()
}

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    let rowHeight:CGFloat = 44
    var rowsHeight:CGFloat = 3 * rowHeight
    /*for i in 0..<cellsHeight.count {
        rowsHeight += CGFloat(cellsHeight[i])
    }*/
    let topHeight = UIApplication.shared.statusBarFrame.height + self.navigationController!.navigationBar.frame.height
    let viewHeight = self.view.frame.height
    let headerHeight:CGFloat = 30
    let height = viewHeight - topHeight - rowsHeight - headerHeight //- 150

    return CGFloat(height)
}

只有一行的高度为 194,另一行的高度为 44。有什么解决问题的想法吗? 谢谢

编辑:

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 3
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let dateFormatter = DateFormatter()
    //dateFormatter.dateStyle = DateFormatter.Style.short
    dateFormatter.dateFormat = "dd.MM.yyyy HH:mm"

    if indexPath.row == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "workoutDateCell", for: indexPath) as! WorkoutDateTableViewCell
        cell.typeLabel.text = "Beginn"
        cell.dateDatepicker.date = Date()
        cell.dateDatepicker.tag = indexPath.row
        cell.dateLabel.text = dateFormatter.string(from: cell.dateDatepicker.date)
        cell.selectionStyle = .none
        return cell
    }
    else if indexPath.row == 1 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "workoutDateCell", for: indexPath) as! WorkoutDateTableViewCell
        cell.typeLabel.text = "Ende"
        cell.dateDatepicker.date = Date()
        cell.dateDatepicker.tag = indexPath.row
        cell.dateLabel.text = dateFormatter.string(from: cell.dateDatepicker.date)
        cell.selectionStyle = .none
        return cell
    }
    else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "workoutSportsCell", for: indexPath) as! WorkoutSportsTableViewCell
        cell.sportsLabel.text = "Sportart"
        cell.sportstypeLabel.text = workoutSports[coreData.getSportsIndex()]
        cell.sportsPicker.delegate = self
        cell.sportsPicker.dataSource = self
        cell.sportsPicker.selectRow(coreData.getSportsIndex(), inComponent: 0, animated: false)
        cell.selectionStyle = .none
        return cell
    }
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if selectedCellIndexPath == indexPath.row {
        return selectedCellHeight
    }
    return unselectedCellHeight
}

要存档,您可以只使用单元格而不是页脚视图。

步骤 1: 删除页脚视图。

步骤 2: 添加日期选择器的单元格。

第 3 步: 当您单击开始和结束日期时间单元格时,然后将日期时间单元格插入所选单元格下方并重新加载 table 视图。

第 4 步:希望能解决您的问题。

如果您有任何疑问,请告诉我。

编辑: 根据讨论,您只需要使用 tableFooterViewForSection.

删除额外的单元格分隔符

所以你只需要添加下面一行就可以解决你的问题:

tableView.tableFooterView = UIView(frame: CGRect(x:0, y:0, width:tableView.frame.width, heigth:0))

并删除 func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat 方法。

希望对您有所帮助。