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

时间:2017-05-20 08:27:59

标签: 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

我的代码:

map

只有一行的高度为194而另一行的高度为44.任何想法如何解决问题? THX

编辑:

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

1 个答案:

答案 0 :(得分:2)

要归档此内容,您只能使用单元格而不是页脚视图。

第1步:删除页脚视图。

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

第3步:当您点击“开始”和“结束DateTime单元格,然后在所选单元格下方插入DateTime单元格并重新加载表格视图。

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

希望它会对你有所帮助。

相关问题