Reputation: 167
I try to make a multi section UITableView
. I get it work if I only add two strings into my "overview" array. But when I try to call my class "Player" and "Comepetitions" I don't make it work. I have checked and both classes have elements.
//My player and Comepetitions class
var comepetition = [Comepetitions]() //Tävlingar
var players = [Player]()//Spelare
let sections = ["Tävlingar","Spelare"]
//here I want to replace my strings to my classes (player and Comepetitions class)
var overview = [[Player](),[Comepetitions]()] as [Any]
override func viewDidLoad() {
super.viewDidLoad()
print(overview)
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.sections[section]
}
func numberOfSections(in tableView: UITableView) -> Int {
return self.sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (overview[section] as AnyObject).count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ConconfirmCell", for: indexPath)
cell.textLabel?.text = overview[indexPath.section] as? String
cell.textLabel?.textColor = UIColor.white
cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 20.0)
return cell
}
//All Information how wants to follow the Segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//Segue for start a new game
if segue.identifier == "startNewGameSegue" {
let destVC=segue.destination as! GameViewController
destVC.competitions = comepetition as [Comepetitions]
destVC.players = players
}
}
Upvotes: 0
Views: 818
Reputation: 167
This code works!
var comepetition = [Comepetitions]() //Tävlingar
var players = [Player]()//Spelare
let sections = ["Tävlingar","Spelare"]
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.sections[section]
}
func numberOfSections(in tableView: UITableView) -> Int {
return self.sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (section == 0) {
return comepetition.count
} else {
return players.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ConconfirmCell", for: indexPath)
if (indexPath.section == 0) {
cell.textLabel?.text = comepetition[indexPath.row].comepetitionsOption
}else{
cell.textLabel?.text = players[indexPath.row].name
}
cell.textLabel?.textColor = UIColor.white
cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 20.0)
return cell
}
//All Information how wants to follow the Segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//Segue for start a new game
if segue.identifier == "startNewGameSegue" {
let destVC=segue.destination as! GameViewController
destVC.competitions = comepetition as [Comepetitions]
destVC.players = players
}
}
}
Upvotes: 1
Reputation: 627
I think it's because of this line, it's optional and you should unwrap it but in the code you post there is no optional checking.
var comepetition = [Comepetitions?]()
And could you add the code that has problem because with code you post here the is no way to know witch is going to be section and witch is the items for that section.
Hope this will helps.
Upvotes: 0