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

标签 swift uitableview footer

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

我的初始情况如下:

enter image description here

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

enter image description here

它不显示所有行。

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

enter image description here

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

enter image description here

我的代码:

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
}

最佳答案

要存档,您可以仅使用单元格而不是页脚 View 。

第 1 步:删除页脚 View 。

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

第 3 步:当您点击“开始和结束日期时间”单元格时,将“日期时间”单元格插入所选单元格下方并重新加载表格 View 。

第 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 方法。

希望对你有所帮助。

关于swift - 如何通过更改单元格的高度来创建动态 TableView 页脚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44083641/

相关文章:

ios - Xcode 10 : Load the same . 项目目标和单元测试目标中的xml文件

swift - 在 Swift 中,如何在 View Controller 中找到 UILayoutGuide 对象?

具有扩展类型约束的 Swift 字典

iphone - 根据内容更新 UITableViewCell 的编辑样式

html - 如何使页脚始终位于页面底部?

swift - 检测在自定义表格单元格中按下了哪个按钮

ios - 如何将 UITableView 连接到新创建的 UITableView 类?

ios - 如何使用 UItableViewCell 重用标识符

css - Internet Explorer 7 中的页脚显示问题

html - 如果页面超出正常高度,页脚会 float