Before I add configureCheckmark, the code works, despite the book saying it shouldn’t. Then, when I “correct” the error by adding the function configureCheckmark, the code compiles, but now I have to click twice on a row to change the checkmark state. I’ve triple-checked the code:
func configureCheckmark(for cell: UITableViewCell,
at indexPath: IndexPath) {
var isChecked = false
if indexPath.row == 0 {
isChecked = row0checked
} else if indexPath.row == 1 {
isChecked = row1checked
} else if indexPath.row == 2 {
isChecked = row2checked
} else if indexPath.row == 3 {
isChecked = row3checked
} else if indexPath.row == 4 {
isChecked = row4checked
}
if isChecked {
cell.accessoryType = .checkmark
} else {
cell.accessoryType = .none
}
}
and I also added the call to tableView(_: cellForRowAt:)
Not quite sure about the bit about the code working despite the book saying it shouldn’t - can you point me at the page number where it says the code shouldn’t work and what is it not supposed to do that it does? A little context sometimes helps figure things out
You having to tap a row twice does not sound like an issue with the configureCheckmark method but rather, an old gotcha - adding the tap handling to didDeselectRowAt instead of didSelectRowAt. Those two are easy to confuse. So see if that is what is happening.
If not, then if you could please upload your project as a ZIP file somewhere and provide a link to the ZIP file, I can take a look to see what is going on.
Thanks for the quick reply! In v6 page 217, at arrow toward the bottom, it says “…Initially you have to tap a couple of times on a row to actually make the checkmark go away.” That was not true for me. At that point the checkmarks appeared and disappeared with a single tap. I then added the code on pages 218 and 219, and then at that point, I did have to tap a couple times to add or remove checkmarks.
Since then, I’ve done the “simplify the code” steps on page 220 and 221, and now the checkmarks behave as they should. I have not been saving versions, so I’m afraid I don’t have the code to ZIP and send to you. Looking over what I have now (before the array section, as of page 221), I do not see anything using didDeselectRowAt, so I guess this will remain a mystery.
No worries. I am enjoying your book, particularly this method of teaching. I stopped using a couple other books because they did not give me enough “hands on code” experience. Thanks.
I’m glad that you’re enjoying the book. Not quite sure what happened with your code but since the original code is not there, I guess, as you said, we’ll have to let it remain a mystery