OGeek|极客世界-中国程序员成长平台

标题: ios - 快速滚动时更改UITableView标题高度 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:24
标题: ios - 快速滚动时更改UITableView标题高度

当我以特定偏移量滚动时如何更改我的 tableview 标题高度? 这是我的标题实现,我使用自定义单元格作为标题。 但基本上我要做的是在滚动到某个偏移量时显示和隐藏标题。

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let  cell = tableView.dequeueReusableHeaderFooterViewWithIdentifier("tableViewHeader") as! MyTableViewHeaderCell
    return cell
}

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 70
}

func scrollViewWillBeginDragging(scrollView: UIScrollView) {
    if (scrollView.contentOffset.y>400){
  // i want to try to change the height of header to be 0/hidden
}



Best Answer-推荐答案


我希望这样做来处理标题高度

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    if (myScrollView.contentOffset.y>400){
        return 70
    } else {
        return 0
    }
}

这是管理表格 View 的标题更新

func scrollViewWillBeginDragging(scrollView: UIScrollView) {
    if (scrollView.contentOffset.y>400 && itWasLessThan400 == true) {
        itWasLessThan400 = false
        myTableView.reloadData() //or reload only the needed section if you have more than one sections
    } else if (scrollView.contentOffset.y<400 && itWasLessThan400 == false) {
        itWasLessThan400 = true
        myTableView.reloadData() //or reload only the needed section if you have more than one sections
    } else 
}

并确保在 viewController 中的 viewDidLoad 处设置 itWasLessThan400 = true

我希望这会有所帮助

关于ios - 快速滚动时更改UITableView标题高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39243304/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4