簡體   English   中英

tableview節標題在多截面tableview中不粘

[英]tableview section header is not sticky in multi sectional tableview

我已經在IOS中實現了一個tableview(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
        }
    }
}

每個節標題都將停留在頂部,直到該節有一些行要顯示。 一旦向上滾動該部分的所有行。 節標題將被下一個節標題替換。

在這里,您可以使用兩種解決方案之一。

  1. 而不是表視圖的節頭。 將您的視圖放在UITableView之上。
  2. 您只能使用一個部分並合並其中的所有行。

從描述安裝方式的角度來看,我傾向於認為您正在尋找的是tableViewtableHeaderView ,而不是節頭。

有關更多信息,請參見此問題官方文檔

如果不符合你的要求,你可能要考慮在頂部的自定義視圖tableView ,如描述在這里

如果在顯示任何部分時第一部分必須始終是粘性的,那么我將考慮在tableView頂部放置一個自定義視圖。 使用autoLayout或UIStackView使其用作表頭視圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM