ios - 为什么不同 TableView Controller 中的 titleForheaderInsection 方法不起作用?

标签 ios iphone swift uitableview

为什么 titleForheaderInsection 方法在不同的 TableView Controller 中不起作用?

我在我的应用程序的一个 Storyboard中创建了两个 TableView Controller ,具有不同的功能,一个是 SettingsTableViewControlelr,一个是 CitylistTableViewControlelr,它们应该有不同的部分标题。

tableView:titleForHeaderInSection方法是用来命名section title的,可惜只有SettingsTableViewControlelr中的方法在iOS模拟器中正确出现了,中的方法CitylistTableViewControlelr 不起作用,我在 tableView:titleForHeaderInSection 方法上设置了一个断点,发现该方法甚至没有在 CitylistTableViewControlelr 中调用。下面是我的代码:

SettingsTableViewController

import UIKit

class SettingsTableViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {

override func viewDidLoad() {
    super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, titleForHeaderInSection section:Int) -> String? {
    switch section{
    case 0:
        return"Settings"
    default:
        return nil
    }
}
//the "tableView:titleForHeaderInSection" method in class SettingsTableViewController 
//is called and section title appears on simulator.

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

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("settingsIdentifier", forIndexPath: indexPath) as! UITableViewCell

    return cell
}    
}

CitylistTableViewControlelr

import UIKit

class CityListTableViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {

override func viewDidLoad() {
    super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 2
}

func tableView(tableView: UITableView, titleForheaderInsection section: Int) -> String? {
    switch section {
    case 0:
        return "Top Cities"
    case 1:
        return "Other Cities"
    default:
        return nil
    }
}
//Putting a breakpoint here, and find "tableView:titleForHeaderInSection" method in 
//CityListTableViewController is not even been called, thus the section titles, "Top Cities" & "Other Cities", 
//do not appear in simulator.I have tried to add "override" keyword before the method, but the 
//complier report error says "Method does not override any method from its superclass". 

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

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    switch section{
    case 0:
        return 12
    case 1:
        return 15
    default:
        return 0
    }
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cityListIdentifier", forIndexPath: indexPath) as! UITableViewCell
    switch indexPath.section{
    case 0:
        cell.textLabel!.text=topCitiesList[indexPath.row]
    case 1:
        cell.textLabel!.text=otherCitiesList[indexPath.row]
    default:
        cell.textLabel!.text="unknown"
    }
    return cell
}   
}

My questions are:

  1. 为什么 CityListTableViewController 中没有调用 tableView:titleForHeaderInSection 方法?
  2. 如何更正我的代码,使章节标题分别显示在模拟器/iPhone 上?

最佳答案

看看两者的区别:

在调用它的第一个 View Controller 中,函数声明如下:

override func tableView(tableView: UITableView, titleForHeaderInSection section:Int) -> String? {
    switch section{

而在没有调用它的那个中,它是这样声明的:

func tableView(tableView: UITableView, titleForheaderInsection section: Int) -> String? {
    switch section {

您缺少覆盖声明。此外,您没有在第二个声明中将“ header ”大写(感谢 Jesper 指出这一点)。

关于ios - 为什么不同 TableView Controller 中的 titleForheaderInsection 方法不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32345661/

相关文章:

ios - Heroku 状态 503 应用程序崩溃

ios - Xcode 4 单元测试链接器错误

iphone - 释放 NSData 导致异常

iPhone - 将两个 wav/caf 文件合并为一个

ios - 如何在快速按下 Collection View 单元格时修复自定义 View 中的约束

ios - UIView不覆盖状态栏区域

ios - UISplitview 方向在 iOS 5 和 iOS 5.1 中没有改变

ios - 使用 iAds 提交新应用

ios - 标签栏应用程序中的 iAd 横幅

iphone - 如何实现单指旋转手势识别器?