Beginning Table Views · Challenge: Updating the Controller | Ray Wenderlich


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5995-beginning-table-views/lessons/18

when we are doing cell.accessorytype = .none or .checkmark isn’t it sufficient to make the cell checked or unchecked , WHY are we doing todoList.todos[indexpath.row].checked = !of the same thing

i mean we are repeating the same thing !!
Somebody help me and reply asap

You are actually doing two different things. First, you are updating the cell to show either a checkmark or no checkmark. The second thing you are doing is updating the class which represents that checkmark.

If you didn’t do the second part, then you would never be able to uncheck the checkmark. Try it out. Delete the line of code after the if block and run it. You’ll quickly see why it’s necessary.

I hope that helps!

1 Like

Hi, @bdmoakley! Why do we use todoList.todos[indexPath.row].checked = !isChecked inside configureCheckmark? This command inverts initial state of checklistItem and creates buggy behavior when table cell appears on screen every time when the application reuses it. Looks like we need set todoList.todos[indexPath.row].checked inside tableView(didSelectRowAt) only.

1 Like

Looks like a bug that I missed. I’ll take a look and see what I can do. Thanks for letting me know.

1 Like

Hey!
In the end of lesson:
Why I cannot type something like: isChecked = !isChecked ?
Before the transformation it looked like row0Item.checked = !row0Item.checked, which looks similar with my suggestion above.

@ollybess Do you still have issues with this?

Hi

Could explain a bit more what this one:
if let label = cell.viewWithTag(1000) as? UILabel {

does.

Here’s the breakdown. What you are doing is getting a plain old UIView back from the method call, viewWithTag(1000). iOS will search for a view with a tag assigned as 1000. That view might not exist, so you use if-left binding.

That is, if a view is returned it will be populated in the label variable. The problem with viewWithTag is that it returns a regular UIView. You need to access a UILabel instead which is why you do a as? UILabel cast. If the view fails to cast to a label, then the code will skip over the code block.

I hope that helps!

I agree. In the cellforrowat we could have used the checked value from the model to show/hide the checkmark instead of calling configureCheckmark (which ends up updating the model and reversing the checkmark). I think we’d also need to have initialised the todoList CheckListItems to all be checked = true for this to work as well as giving them some text.
Then configureCheckmark only needs to be called from didselectrowat.

@hashmo Thank you for sharing your solution - much appreciated! :]

1 Like

Hi. Why do you initialise the TodoList in the viewcontroller init instead of just setting it at the variable definition var todoList = TodoList()?