[英]Why are tableview section header views appearing over the table header view?
[英]why I am not getting the not visible section header views in a tableView?
这是我的 viewController,正如您在加载时所看到的,只有三个标题视图可见。
为什么我没有得到位于初始视图下方的那些标题的 headerView 文本标签文本。 我总共有 6 个部分,它们总共对应于 6 个标题视图。
这是我的代码:
//
// 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
}
}
打印到控制台:
标题信息
教育
工作经验
未检索标题
未检索标题
未检索标题
我只能检索 6 个部分标题中的 3 个。 我错过了什么?
这是预期的行为。
屏幕外标题视图将不存在,因此headerViewForSection
将为该部分返回 nil。
从UITableView
类参考:
返回值
与该部分关联的标题视图,如果该部分没有标题视图,则为 nil。
如果你滚动你的tableView
,你会看到tableView
在它们出现在屏幕上时创建标题视图,并在它们滚动到屏幕外时释放它们。
您可以通过记录返回给您的视图的地址来确定这一点。 在屏幕外滚动的部分现在返回 nil,而现在在屏幕上滚动的部分现在返回一个视图。
如果您在屏幕外滚动部分标题,然后又回到屏幕上,tableView 实际上会为该部分返回一个不同的视图,因为原始视图已被释放,并创建了一个新视图。
如果您需要屏幕外标题的标题,则必须从模型(或数据源)中检索它们,因为这些部分不存在标题。
您可能需要考虑实现tableView:titleForHeaderInSection:
以避免必须访问 headerView 的 textLabel 来获取标题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.