почему 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
}
}
Мои вопросы:
- Почему
tableView:titleForHeaderInSection
метод не может быть вызван вCityListTableViewController
? - Как я могу исправить свой код, чтобы заголовок раздела отображался на симуляторе/iPhone соответственно?
спасибо за ваш ответ. Я попытался добавить ключевое слово «переопределить» перед этим методом, но ошибка отчета об уступчивости говорит: «Метод не отменяет какой-либо метод из его суперкласса». – stephen
Вы написали его 'titleForheaderInsection', а не' titleForHeaderInSection'. Селекторы чувствительны к регистру. – Jesper
Спасибо @Jesper добавил исправление – Yarneo