我是编程新手,Swift是我学习的第一门编程语言,如果问题的答案太明显,我深表歉意。我的应用程序需要添加的球员数量以及用户希望将球员分成的球队数量。对于偶数,我的tableView工作得很好,如果用户插入6名球员并选择2支球队,他们将被分成2支3人的球队,以此类推。但如果用户插入7名球员并将其分成2支队伍,它也会这样做,但不会出现额外的球员。我希望奇数玩家总是出现在表View的最后一部分,用户插入的任何奇数玩家,而其余部分都是偶数。我在我的功能单元ForRowAt中尝试了不同的方法来实现这一点,但没有成功。这是我的代码:
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
}
}
假设我们有11 players
和3 teams
。。。
如果我们使用整数除法计算每支球队的球员数量,我们得到11 / 3 == 3
。。。但是由3名球员组成的3支球队(部分(只让我们有9名球员;剩余的";玩家。
因此,我们想要3个部分的结果,包含:
3 players
4 players (3 plus one of the leftovers)
4 players (3 plus one of the leftovers)
首先,让我们得到每支球队球员的Integer
值:
// total number of players
let numPlayers = shuffledPlayers.count
// Integer value of players per team
let playersPerTeam = numPlayers / numberOfTeams
接下来,我们将使用Modulo运算符来获得";额外玩家":
// modulo (remainder) of "extra players"
let mod: Int = numPlayers % numberOfTeams
在这个由11名球员/3支球队组成的例子中,mod == 2
,这意味着最后两支球队各需要一名额外的球员。
因此,我们检查团队(section
(是否大于或等于团队的数量减去"0"的计数;额外玩家":
// if section is greater than or equal to
// number of teams minus "extra players"
if section >= numberOfTeams - mod {
// return players per team Plus 1
return playersPerTeam + 1
}
// else return players per team
return playersPerTeam
完整的函数现在看起来是这样的:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// total number of players
let numPlayers = shuffledPlayers.count
// Integer value of players per team
let playersPerTeam = numPlayers / numberOfTeams
// modulo (remainder) of "extra players"
let mod: Int = numPlayers % numberOfTeams
// if section is greater than or equal to
// number of teams minus "extra players"
if section >= numberOfTeams - mod {
// return players per team Plus 1
return playersPerTeam + 1
}
// else return players per team
return playersPerTeam
}
当然,我假设您正在进行自己的用户输入验证,以避免类似";7名球员分成12支球队";。
//there is the problem, your number of rows in last section is wrong
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return shuffledPlayers.count / numberOfTeams
// if shuffledPlayers.count = 7 and numberOfTeams = 2, the result is 3
//so you should do something like that:
/*
* if section == numberOfTeams {
* return shuffledPlayers.count % numberOfTeams + shuffledOlayers.count / numberOfTeams
* } else {
* return shuffledOlayers.count / numberOfTeams
* }
*/
}