使 tableView 显示偶数和奇数单元格标题 Swift
我是编程新手,Swift 是我正在学习的第一种编程语言,如果问题的答案太明显,我深表歉意。 我的应用程序需要添加玩家数量以及用户希望将玩家分成的团队数量。对于偶数,我的 tableView 工作正常,如果用户插入 6 名球员并选择 2 支球队,他们将被分为 2 队,每队 3 人,依此类推,如果用户插入 7 名球员并将其分为 2 队,则会执行以下操作:相同,但额外的玩家不会出现。我希望奇数数量的玩家始终出现在 tableView 的最后部分中,用户插入的任意奇数数量的玩家同时其余部分是偶数。我在 func cellForRowAt 中尝试了不同的方法来实现此目的,但没有成功。这是我的代码:
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
}
}
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
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设我们有
11 名球员
和3 支球队
...如果我们使用整数除法计算每支球队的球员数量,我们会得到
11 / 3 == 3
...但是 3 名球员组成的 3 支球队(分区)只能让我们得到 9 名球员,而且我们还有 2 名“剩余”球员。因此,我们需要 3 个部分的结果,其中包含:
首先,让我们获取每支球队的球员的
Integer
值:接下来,我们将使用 Modulo 运算符来获取“额外球员”
:以 11 名球员/ 3 支球队为例,
mod == 2
,这意味着最后 2 支球队各需要一名额外的球员。因此,我们检查团队(
section
)是否大于或等于团队数量减去“额外玩家”的数量:完整的功能现在如下所示:
当然,我假设您正在执行自己的用户输入验证,以避免出现“7 名玩家分成 12 支球队”之类的情况。
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".