Chapter 20 Page 855 Exercise

I’m stumped on this first exercise…

Exercise: Display the due date in the table view cells, under the text of the to-do item.”

Earlier in this chapter we added code to AllListsViewController’s cellForRowAt to show items remaining as a subtitle. My thought was to basically do the same thing in ChecklistViewController’s cellForRowAt to display the due date as a subtitle, but the code is so different from AllListsViewController that I’m not sure how it would work.

While browsing the forums earlier I saw a closed topic where someone added a second label in storyboard to make this work, but I don’t see why thats necessary considering I didn’t have to do that in AllListsViewController to display the remaining items subtitle.

If someone could point me in the right direction here it would be much appreciated :slightly_smiling_face:

Back in chapter 19, you made a change in AllListsViewController, to allow you to display the subtitle. Look for this line of code in tableView(_:cellForRowAt:) :

 cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellIdentifier)

In the AllListsViewController, you are determining the content of the TableViewCell in code.

For the ChecklistViewController, you are instead using a custom prototype cell in the storyboard. To have a title there, you had to put a label on the prototype cell. So to display a subtitle, you will need to put another label on the prototype.

1 Like

That makes a lot of sense thanks for explaining that to me.

I added the label in storyboard. I tried updating the label from ChecklistViewController configureText(for:with:) and I get this error.

I’m trying to figure out how to convert type Date to String. I know we used the DateFormatter() in ItemDetailViewController and I tried doing something similar in ChecklistViewController but with no luck.

What did you try? It will be easier to help if you show what you have so far.

func updateSubLabel() {
let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeStyle = .short
subLabel.text = formatter.string(from: ) // Not sure what goes here
}

And I put this in ChecklistViewController. I’m thinking maybe I need to add an instance variable but I’m not sure…
I also created an outlet for the second label called subLabel but wasn’t sure if I needed that since there is no outlet for the first label because it was referenced using a tag.

The outlet for the sub label won’t really work, because the checklist can have more than one row. You need the sub label for the current cell, and there is one for each row. So you should do the tag thing again to get the sub label of the cell you are configuring, like you were doing in the code with the error.

Your formatter just needs the item.dueDate, like you were doing before, so the line looks like

 subLabel.text = formatter.string(from: item.dueDate)
1 Like

I got it thank you so much, I didn’t realize the formatter code needed to be within the configureText method.

In the simulator, I add an item to the checklist without a reminder. Then I press the detail disclosure button and add a reminder but label doesn’t update until I press the back button then go back into the list.

I need to update the label when I’m finished editing which would make sense to do in ChecklistViewController ItemDetailViewController(_:didFinishEditing:) but I’m not sure how to do it since all the code to update the label is in configureText(for:with:)

It looks to me like it should be updating. You have a call to configureText(…) in your didFinishEditing method.

Are you sure the didFinishEditing method is getting called? I would put a break point on the first line, and on the configureText(…) line, to see if it is actually doing those things.

Does the main text get updated if you edit it in the detail?

Sorry about that, I just ran the app again and everything is working fine. Not sure why it was giving me those issues before but all seems to be good. Thank you.

This topic was automatically closed after 166 days. New replies are no longer allowed.