I have been trying to do what you proposed. I think I almost got it.
1. I setup a global struct
struct Data {
var num: Int = 0
var name: String = ""
var strikeThrough: Bool = false
var color: Bool = false
var accessory: Bool = false
}
var objectsArray = [Data]()
2. I setup a global function
func quarterBacks() {
objectsArray = [
Data(num: 1, name: "Patrick Mahomes, KC", strikeThrough: false, color: false, accessory: false),
Data(num: 2, name: "Deshaun Watson, HOU", strikeThrough: false, color: false, accessory: false),
Data(num: 3, name: "Aaron Rodgers, GB", strikeThrough: false, color: false, accessory: false),
Data(num: 4, name: "Matt Ryan, ATL", strikeThrough: false, color: false, accessory: false),
Data(num: 5, name: "Baker Mayfield, CLE", strikeThrough: false, color: false, accessory: false),
Data(num: 6, name: "Carson Wentz, PHI", strikeThrough: false, color: false, accessory: false),
Data(num: 7, name: "Jared Goff, LAR", strikeThrough: false, color: false, accessory: false),
Data(num: 8, name: "Cam Newton, CAR", strikeThrough: false, color: false, accessory: false),
Data(num: 9, name: "Andrew Luck, IND", strikeThrough: false, color: false, accessory: false),
Data(num: 10, name: "Drew Brees, NO", strikeThrough: false, color: false, accessory: false),
Data(num: 11, name: "Ben Roethlisberger, PIT", strikeThrough: false, color: false, accessory: false),
Data(num: 12, name: "Dak Prescott, DAL", strikeThrough: false, color: false, accessory: false),
Data(num: 13, name: "Russell Wilson, SEA", strikeThrough: false, color: false, accessory: false),
Data(num: 14, name: "Tom Brady, NE", strikeThrough: false, color: false, accessory: false),
Data(num: 15, name: "Lamar Jackson, BAL", strikeThrough: false, color: false, accessory: false),
Data(num: 16, name: "Mitchell Trubisky, CHI", strikeThrough: false, color: false, accessory: false),
Data(num: 17, name: "Jameis Winston, TB", strikeThrough: false, color: false, accessory: false),
Data(num: 18, name: "Philip Rivers, LAC", strikeThrough: false, color: false, accessory: false),
Data(num: 19, name: "Kirk Cousins, MIN", strikeThrough: false, color: false, accessory: false),
Data(num: 20, name: "Derek Carr, OAK", strikeThrough: false, color: false, accessory: false),
Data(num: 21, name: "Sam Darnold, NYJ", strikeThrough: false, color: false, accessory: false),
Data(num: 22, name: "Josh Allen, BUF", strikeThrough: false, color: false, accessory: false),
Data(num: 23, name: "Matthew Stafford, DET", strikeThrough: false, color: false, accessory: false),
Data(num: 24, name: "Marcus Mariota, TEN", strikeThrough: false, color: false, accessory: false),
Data(num: 25, name: "Jimmy Garoppolo, SF", strikeThrough: false, color: false, accessory: false),
Data(num: 26, name: "Andy Dalton, CIN", strikeThrough: false, color: false, accessory: false),
Data(num: 27, name: "Eli Manning, NYG", strikeThrough: false, color: false, accessory: false),
Data(num: 28, name: "Nick Foles, JAC", strikeThrough: false, color: false, accessory: false),
Data(num: 29, name: "Joe Flacco, DEN", strikeThrough: false, color: false, accessory: false),
Data(num: 30, name: "Ryan Fitzpatrick, MIA", strikeThrough: false, color: false, accessory: false)
]
}
3. I call quarterbacks() in viewDidLoad()
override func viewDidLoad() {
super.viewDidLoad()
quarterBacks()
}
4. I set the structs data in my three functions addPlayer(index: IndexPath), removePlayer(index: IndexPath), and markAsTaken(index: IndexPath)
func addPlayer(index: IndexPath) {
_ = top30QuarterbacksTable.cellForRow(at: index)
let str: String = objectsArray[index.row].name
objectsArray[index.row].strikeThrough = true
objectsArray[index.row].accessory = true
objectsArray[index.row].color = true
if myRoster == [] {
myRoster?.insert(str, at: 0)
} else {
myRoster?.append(str)
}
if draftOrder == [] {
draftOrder?.insert(str, at: 0)
} else {
draftOrder?.append(str)
}
top30QuarterbacksTable.reloadData()
}
func removePlayer(index: IndexPath) {
_ = top30QuarterbacksTable.cellForRow(at: index)
let str: [String] = [objectsArray[index.row].name]
objectsArray[index.row].strikeThrough = false
objectsArray[index.row].color = false
objectsArray[index.row].accessory = false
myRoster?.removeAll(where: { str.contains($0) })
draftOrder?.removeAll(where: { str.contains($0) })
top30QuarterbacksTable.reloadData()
}
func markAsTaken(index: IndexPath) {
_ = top30QuarterbacksTable.cellForRow(at: index)
let str: String = objectsArray[index.row].name
objectsArray[index.row].strikeThrough = true
objectsArray[index.row].color = true
objectsArray[index.row].accessory = false
if draftOrder == [] {
draftOrder?.insert(str, at: 0)
} else {
draftOrder?.append(str)
}
top30QuarterbacksTable.reloadData()
}
5. I setup cellForRowAt with if statements
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let num = objectsArray[indexPath.row].num
let name = objectsArray[indexPath.row].name
cell.textLabel?.text = "\(num). \(name)"
cell.textLabel?.adjustsFontSizeToFitWidth = true
cell.textLabel?.font = UIFont.systemFont(ofSize: 22)
if objectsArray[indexPath.row].strikeThrough == false && objectsArray[indexPath.row].accessory == false && objectsArray[indexPath.row].color == false {
cell.textLabel?.text = "\(num). \(name)"
cell.textLabel?.attributedText = noStrikeThroughText(name)
cell.accessoryType = UITableViewCell.AccessoryType.none
cell.backgroundColor = .none
}
else if objectsArray[indexPath.row].strikeThrough == true && objectsArray[indexPath.row].accessory == true && objectsArray[indexPath.row].color == true {
cell.textLabel?.text = "\(num). \(name)"
cell.textLabel?.attributedText = strikeThroughText(name)
cell.accessoryType = UITableViewCell.AccessoryType.checkmark
cell.backgroundColor = .systemGray3
} else {
cell.textLabel?.text = "\(num). \(name)"
cell.textLabel?.attributedText = strikeThroughText(name)
cell.accessoryType = UITableViewCell.AccessoryType.none
cell.backgroundColor = .systemGray2
}
return cell
}
It works perfectly except that when I added the if statements in cellForRowAt it doesn't display the num.
I want it to look like this:
1. Patrick Mahomes, KC
2. Deshaun Watson, HOU
3. Aaron Rodgers, GB
...
but it looks like this:
Patrick Mahomes, KC
Deshaun Watson, Hou
Aaron Rodgers, GB
...
All other functionalities work perfect. This is the only thing I cannot figure out why it is doing this. I remove the if statements from cellForRowAt and it displays the way I want it to but the other functionalities do not work right.
I am sorry if it seemed like I didn't want to do what you proposed but I am trying.