I have managed to get most of my app done. But I’m convinced there is a better way to code the following although my version does work, with one small error. So wondered if someone could point out the best or a better way to fix my problem. It didn’t feel overly right when I was coding it.
I have a Table with three sections.
1 => Playing
2 => Subs
3 => Unavailable
My model is just of type [[Player]]
. On the viewDidLoad() I order these objects and put the first 11 in the playing section playingList[0]
, the remainder go into the substitute section playingList[1]
. As the game plays, I update a label on the cell with how long this person has played. This is a separate piece of data. I have a dictionary playedTimePersonDictionary
[String: Int]
(as a side note, I toyed with making this of type [Player:Int]
. I am going to store this dictionary as the player:MinutesPlayed in an NSSet.
Every second I am updating my timer and updating the cells of the table like so:
for cells in playerTableView.visibleCells {
let cell = cells as! MatchPlayerCell
if let playerLabel = playedTimePersonDictionary[cell.playerName.text!]{
for player in playingList[0]{
if player.name == cell.playerName.text {
cell.configurePlayedTimeLabel(playerLabel, hex: "playing")
}
}
for player in playingList[1]{
if player.name == cell.playerName.text {
cell.configurePlayedTimeLabel(playerLabel, hex: "substitute")
}
}
for player in playingList[2]{
if player.name == cell.playerName.text {
cell.configurePlayedTimeLabel(0, hex: "unavailable")
}
Each hex
value relates to a different UIColor
label.
In this function, I update the Dictionary of playing Time by they key (The player’s name) and the value (Seconds played).
But when I come to update their label on the cell, show how long they’ve played. It works but, in my simulator all the cells update with a label, as if they are all playing. Which they are not, I’ve echoed out the contents of the playingList to confirm.
But When I run my app on my phone, it works, except the very last row in my table shows a value, like they are playing. But they aren’t, so not sure what is going on or the best approach?