2015-09-02 2 views
0

почему titleForheaderInsection способ в другой настольный контроллер не работает?Почему метод titleForheaderInsection в другом контроллере таблиц не работает?

Я создаю два настольных контроллера в одной раскадровке для своего приложения, с другой функцией, один - SettingsTableViewControlelr, один - CitylistTableViewControlelr, и у них должны быть разные заголовки разделов.

tableView:titleForHeaderInSection метод используется для заголовка название раздела, но, к сожалению только метод в 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 
} 
} 

Мои вопросы:

  1. Почему tableView:titleForHeaderInSection метод не может быть вызван в CityListTableViewController?
  2. Как я могу исправить свой код, чтобы заголовок раздела отображался на симуляторе/iPhone соответственно?

ответ

2

Посмотрите на различия между двумя:

В первом контроллере представления, где она вызывается, то функция объявляется так:

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

В то время как в одном он не называется, он объявляется следующим образом:

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

Вам не хватает декларации переопределения. Кроме того, вы не использовали «заголовок» во втором объявлении (спасибо, что указали это на Jesper).

+0

спасибо за ваш ответ. Я попытался добавить ключевое слово «переопределить» перед этим методом, но ошибка отчета об уступчивости говорит: «Метод не отменяет какой-либо метод из его суперкласса». – stephen

+0

Вы написали его 'titleForheaderInsection', а не' titleForHeaderInSection'. Селекторы чувствительны к регистру. – Jesper

+0

Спасибо @Jesper добавил исправление – Yarneo