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
}
}
2
Answers
Let’s say we have
11 players
and3 teams
…If we calculate the number of players per team using Integer division, we get
11 / 3 == 3
… but 3 teams (sections) of 3 players only gets us to 9 players, and we have 2 "leftover" players.So, we want a result of 3 sections, containing:
First, let’s get the
Integer
value of players per team:Next, we’ll use the Modulo operator to get the "extra players":
In this example of 11 players / 3 teams,
mod == 2
, which means the last 2 teams each need an extra player.So, we check to see if the team (
section
) is greater than or equal to the number of teams minus the count of "extra players":The full func now looks like this:
Of course, I’m assuming you’re doing your own user-entry validation, to avoid something like "7 players split into 12 teams".