I am new to programming and Swift is my first programming language that I am learning, I apologize if the answer to the question is too obvious. My app requires amount of players to be added and number of teams the user wishes to divide the players into. With even numbers my tableView works fine, if the user inserts 6 players and selects 2 teams they will be divided into 2 teams of 3 and so on with even numbers but if the user inserts 7 players and divides it into 2 teams it will do the same but the extra player will not appear. I wish for the the odd number of players to always appear in the last section of the tableView with any odd number of players the user inserts while the rest of the sections are even. I've tried different things in my func cellForRowAt to achieve this but with no luck. Here is my code:
import UIKit
import CoreData
import ChameleonFramework
class ShuffledTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.tintColor = UIColor.yellow
loadedShuffledPlayers()
tableView.reloadData()
tableView.register(UINib(nibName: "ShuffledTableViewCell", bundle: nil), forCellReuseIdentifier: "shuffledCell")
}
//Player comes from core data
var shuffledPlayers = [Player]()
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
var numberOfTeams = Int()
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return shuffledPlayers.count / numberOfTeams
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "shuffledCell", for: indexPath) as! ShuffledTableViewCell
let rowsPerSection = shuffledPlayers.count / numberOfTeams
let rowInSection = indexPath.row + rowsPerSection * indexPath.section
cell.shuffledLabel.text = shuffledPlayers[rowInSection].names
cell.shuffledLabel.textColor = UIColor.randomFlat().lighten(byPercentage: 10)
cell.backgroundColor = UIColor.black
return cell
}
func loadedShuffledPlayers(){
let request: NSFetchRequest<Player> = Player.fetchRequest()
do{
shuffledPlayers = try context.fetch(request).shuffled()
}catch{
print("Error loading data from core data. \(error)")
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
return numberOfTeams
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Team # \(section + 1)"
}
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){
let header = view as! UITableViewHeaderFooterView
header.textLabel?.textColor = UIColor.white
}
}