How do I save the view of a table view. I use a strike through text, an accessory type check, and other attributes to the cell at indexPath. How do I save it? I can't even switch to a different view without it not staying.
hi,
there are a whole bunch of things going on here that you need to address first, before moving along to any discussion of persisting data (if that's really what you mean, as in saving to disk).
(1) your data consists only of objectsArray: [String], but once the tableview is drawn for you, you try to then store/update the state of the objects in the attributedText field of each cell.
that's bad. you should perhaps keep a separate, parallel array to keep track of the additional state of each string in the object array. or, better still, your objectsArray should be an array of structs, where each struct is something like
struct Quarterback {
var name: String
var strikeThrough: Bool
}
when cellForRowAtIndexPath is called, get the information from objectsArray and fill in both the text and attributed text of the cell you have dequeued. example sequence:
let cell = top30QuarterbacksTable.cellForRow(at: indexPath)
let player: Quarterback = objectsArray[indexPath.row]
cell.textLabel?.text = "\(indexPath.row+1). " + player.name
cell.textLabel?.adjustsFontSizeToFitWidth = true
cell.textLabel?.font = UIFont.systemFont(ofSize: 22)
if player.strikeThrough {
cell?.textLabel?.attributedText = strikeThroughText(player.name)
} else {
cell?.textLabel?.attributedText = nil
}
return cell
you're also apparently trying to tweak a cell's accessoryType elsewhere in your code -- that's something that should also set up here and be tracked in the Quarterback struct by another variable.
(2) if you want to know what's in a given row of the table at anytime, don't ask cellForRowAtIndexPath for the cell (note: the cell you get back will not be the cell that's displayed on screen, but only a new cell with the information you're looking for that you already have). you know what will be in the row just by looking at the objectsArray. and cellForRowAtIndexPath is really something you want to let the system call -- it's not something you want to call on your own.
an example (just kill the commented-out lines)
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
// let cell = self.top30QuarterbacksTable.cellForRow(at: indexPath)
// let str: String = (cell?.textLabel!.text)!
let player: Quarterback = objectsArray[indexPath.row]
// if cell?.textLabel?.attributedText == strikeThroughText(str) {
// strikeThroughTextBool = true
// } else {
// strikeThroughTextBool = false
// }
// if strikeThroughTextBool == false
if player.strikeThrough {
......
}
note that addPlayer and RemovePlayer have the same issues at the start of their code.
(3) call top30QuarterbacksTable.reloadData() whenever you make any changes to the objectsArray -- don't try to do anything with cells directly. reloadData() will cause all the cells to be redrawn.
for example, each of your addPlayer and removePlayer should end with a call to reloadData().
hope that helps get you started ... happy to discuss more when you clean up some of these issues.
hope that helps,
DMG