【问题标题】:tableview section header is not sticky in multi sectional tableviewtableview 部分标题在多部分 tableview 中不粘
【发布时间】:2018-12-24 09:15:04
【问题描述】:

我已经在 IOS 中实现了一个表格视图(4 个部分)。问题是 我刚刚在第一部分添加了一个部分标题。其他部分没有标题。第一部分没有行(行数为 0)。其他部分有多行。当我滚动时,第一部分的标题不粘.它正在滚动并超出屏幕。我的表格视图样式很简单。我怎样才能让第一部分的标题始终保持粘性。代码在下面。不幸的是,表格视图太复杂了,我不想让它只有一个部分所以我已经实现了多部分。

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    if section == 0 {
        return tabHeaderView.contentView
    }
    else {
        return UIView()
    }
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    if section == 0 {
      return UITableView.automaticDimension
    }
    else {
        return 0 //just first section have a header (tabview)
    }
}

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return CGFloat.leastNormalMagnitude
}

func numberOfSections(in tableView: UITableView) -> Int {
    if dataReady {
        totalSectionCount = getSectionCount()
        return totalSectionCount
    }
    else {
        return 1 //permanent because of tabview
    }
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    if !dataReady || ApplicationContext.instance.userAuthenticationStatus.value == .semiSecure{
        return 1//for shimmer cell and semisecure view
    }
    else {
        return getNumberOfRows(sectionNumber: section)
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if ApplicationContext.instance.userAuthenticationStatus.value == .semiSecure {
        semisecureViewCell = EarningsSemisecureViewCell()
        setSemisecureViewTextInfo(cell: semisecureViewCell)
        semisecureViewCell.delegate = self
        semisecureViewCell.layoutIfNeeded()
        return semisecureViewCell
    }
    else if !dataReady {
        return getShimmerCell()
    }
    else {
        if indexPath.row == 0 && !(selectedTabIndex == .BRANDPOINT && indexPath.section == 1){//marka puan listesinde header cell olmayacak
            return getSectionHeaderViewCell(tableView: tableView,sectionNumber: indexPath.section)
        }
        else {
            let cell = getTableViewCell(tableView: tableView, indexPath: indexPath)
            cell.layoutIfNeeded()
            return cell
        }
    }
}

【问题讨论】:

  • 我编辑了。第一节 ehder 必须是粘性的。
  • getSectionCount()方法的代码在哪里?
  • 对不起,我忘记了。关于节数没有问题,但我已将代码更改为仅一个节,并且我管理了一节中的所有单元格。它已完成。感谢您的回答。跨度>

标签: ios uitableview uitableviewsectionheader


【解决方案1】:

每个部分的标题都将固定在顶部,直到该部分有一些行要显示。向上滚动该部分的所有行后。节标题将被下一个节标题替换。

在这里您可以使用两种解决方案之一。

  1. 而不是表视图部分标题。将您的视图放在 UITableView 之上。
  2. 您只能使用一个部分并将其中的所有行组合起来。

【讨论】:

  • 我还有一个 tableheaderview。我认为你是对的。我必须将所有内容合并到一个部分中。非常感谢:)
  • 解决方案 1 很好。 :) 将视图放在 tableView 之上,或者将视图添加到 tableView 的同一个父视图。
【解决方案2】:

根据您描述设置的方式,我倾向于认为您要查找的是tableViewtableHeaderView,而不是部分标题。

请参阅this questionofficial documentation 了解更多信息。

如果这不符合您的要求,您可能需要考虑在tableView 之上的自定义视图,如here 所述。

【讨论】:

    【解决方案3】:

    如果在显示任何部分时第一部分必须始终保持粘性,我会考虑在 tableView 顶部放置一个自定义视图。使用 autoLayout 或 UIStackView 让它作为表头视图工作。

    【讨论】:

    • 我还有一个tableheaderview。我想我必须将所有部分组合在一起
    猜你喜欢
    • 2012-06-27
    • 1970-01-01
    • 1970-01-01
    • 2015-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多