为什么我在 tableView 中看不到不可见部分 header 视图?

why I am not getting the not visible section header views in a tableView?

这是我的 viewController,正如您在加载时看到的那样,只有三个 header 视图可见。

为什么我没有获得位于初始视图下方的 header 的 headerView textlabel 文本。我总共得到了 6 个部分,总共对应了 6 header 次浏览。

这是我的代码:

    //
//  FillinTheBlanksTableViewController.swift
//  GetJobbed
//
//  Created by Rustam Allakov on 9/22/15.
//  Copyright (c) 2015 Rustam Allakov. All rights reserved.
//

import UIKit

class FillinTheBlanksTableViewController: UITableViewController {

   override func viewDidLoad() {
      super.viewDidLoad()



   }


   override func viewDidAppear(animated: Bool) {
      super.viewDidAppear(animated)


      //
      //      print("the first section name is: ")
      //      println(tableView.headerViewForSection(0)!.textLabel.text!)

      let headerNames = getSectionNames()

      println(headerNames)

      print("the number of sections in a tableview ")
      println(tableView.numberOfSections())
   }

   //get all the sections you got
   func getVisibleSectionNames () -> [String] {

      var headerNames = [String]()
      if let indexPaths = tableView.indexPathsForVisibleRows() as? [NSIndexPath] {

         headerNames = indexPaths.map { [unowned self] indexPath -> String in
            let section = indexPath.section
            if let headerText = self.tableView.headerViewForSection(section) {
               return headerText.textLabel.text!

            } else {
               return ""
            }
         }
      }

      return headerNames

   }
   ///array of strings with names of the headerViews in a tableview
   //why I am not getting the not visible part of my table view?
   func getSectionNames() -> [String] {
      var sectionNames = [String]()
      //how many section do my table view got ?
      for i in 0..<tableView.numberOfSections() {
//         if let headerView = tableView.headerViewForSection(i) {
//            println("the header number \(i)")
//            sectionNames.append(headerView.textLabel.text!)
//         } else {
//            println("i am not getting these \(i)")
//            
//         }

         let headerView = tableView.headerViewForSection(i)
         sectionNames.append(headerView?.textLabel.text ?? "not retreived header")

      }

      return sectionNames

   }





}

打印到控制台:

HEADER INFORMATION
EDUCATION
WORK EXPERIENCE
not retreived header
not retrieved header
not retreived header

我只能检索 6 个部分中的 3 个 header 标题。我错过了什么?

这是预期的行为。

off-screen header 视图将不存在,因此 headerViewForSection 将为该部分 return 零。

来自UITableViewclassreference:

Return Value

The header view associated with the section, or nil if the section does not have a header view.

如果您滚动 tableView,您会看到 tableView 在出现 on-screen 时创建 header 视图,并在它们滚动后释放它们 off-screen.

您可以通过记录 returned 给您的视图地址来确定这一点。滚动 off-screen 的部分现在 return 为零,而现在滚动 on-screen 的部分现在 return 一个视图。

如果您滚动某个部分 header off-screen,然后返回屏幕,tableView 实际上 return 是该部分的不同视图,因为原始视图被释放,并创建了一个新的。

如果您需要 off-screen header 的标题,您必须从模型(或数据源)中检索它们,因为没有 headers这些部分将存在。

您可能需要考虑实施 tableView:titleForHeaderInSection: 以避免必须访问 headerView 的 textLabel 才能获取标题。