Beginning Table Views · Using Sections | Ray Wenderlich


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

Brian, faithfully following your tutorial, this error shows up:

.
I have copied the viewDidLoad from your finished Xcode file were the error is not showing.
Any suggestions?
Tom

Sorry, solved at 05:22 in the video😌.

Hi Tom - quick question, what version of Xcode are you using?

Brian: Version 10.0 beta 6 (10L232m).

Hi Tom - Sorry that I missed your response notification. In ChecklistItem, did you add an @objc before the text property? You need to expose the property in objective c. Give that a shot and let me know how it works out. Thanks!

@bdmoakley I missed some further explanation of the #selector syntax. I can’t remember that it has been explained before in the iOS and Swift for Beginners path. (I can imagine what it does but some more details would be really great.)

@hettiger You can read more about selectors here:

I hope it helps!

It would be nice to have this #selector syntax explained in reference to this particular code we are writing. It kind of just pops out of nowhere, and it is assumed we all know what we’re talking about. For example, where is this ‘getter’ coming from? How would I know to type ‘getter’ in the first place? Things have been going well but this section is quite confusing as it lacks clear explanation as to where things are coming from, and the tone changes to assuming a beginner even knows what things like ‘collation’ mean. I’ve watched this three times now, and I just don’t feel like I’m truly learning what is happening in this video. Just copying the code on screen, but not understanding where it comes from, what exactly it references, or how I would even get to this solution on my own.

I’ve been really enjoying the course, but I bring this to attention as it may be worth another look for future releases.

1 Like

I’ll make sure to pass on the note. Often times, when developing courses, it’s easy to take for granted things like this.

I very much agree with this. This is mean to be a beginners course for people who may have never done any programming and I can imagine the content of this video being very overwhelming.

There’s also stuff earlier on referencing NSCoder which isn’t explained at all, just assumed you know what it is, and why its an argument of an init function

We’re in the planning stages of updating this course. I’ll make sure to pass along your comments.

I’ve been trying to wrap my head around the concepts in this video myself and after some further reading I think I basically understood what’s going on. I went through the code added to viewDidLoad and commented on it - I did this mostly to understand it myself but maybe it helps some people who are struggling with the same issues.

1: Identifying the number of sections we need for our locale (for English: number of letters in the alphabet) - this is done by UILocalizedIndexedCollation:

let sectionTitleCount = UILocalizedIndexedCollation.current().sectionTitles.count

2: Creating an empty array of ChecklistItem arrays (which are set to equal nil) with the number of elements corresponding to the number of sections we need (which we got from the previous line -> 26 sections for 26 letters of the alphabet) - the (repeating:count:) method is just a shortcut used instead of appending the arrays manually:

var allSections = [[ChecklistItem?]?](repeating: nil, count: sectionTitleCount)

3: Preparing for the for-loop (1): Creating a local variable “sectionNumber” that is going to be set to the section number for each item that is currently within the loop’s body (-> “item in todoList.todos”, see below) - defined as a variable because it is reassigned for each iteration of the loop:

var sectionNumber = 0

4: Preparing for the for-loop (2): Create a collation object that holds the list of letters for the current locale (English) which are used as indices for the section titles (as seen in the first line of code above):

let collation = UILocalizedIndexedCollation.current()

5: Initiating the for loop that loops through all items that are in our todos array:

for item in todoList.todos {

6: The sectionNumber (see above) is set to the index of the section title that corresponds to the current todos-item within the loop’s body (example: for “Call Matthew” this would return the integer “2” [here I’m not sure if the result of the call to collation.section is zero-indexed or not], thus corresponding to the third array in the allSections array) - this is determined by the #selector, which uses the ChecklistItem.text (therefore the getter) as a means to sort the items alphabetically.

sectionNumber = collation.section(for: item, collationStringSelector: #selector(getter:ChecklistItem.text))

7: The if-statement checks if the array that has been selected within the allSections array (for our example “Call Matthew” this would be the third array with the index 2) is still empty and thus equals nil (remember: above the array has been initialized with nil values) - in this case the array is set to equal an array that contains the type ChecklistItem:

if allSections[sectionNumber] == nil {
    allSections[sectionNumber] = [ChecklistItem?]()
}

8: The item (for our example: Call Matthew") is appended to its corresponding array; once again as a clarification: the sectionNumber is derived from the letters of the alphabet and have been determined by our UILocalizedIndexedCollation.current() instance.

    allSections[sectionNumber]!.append(item)
}

9 - After the for-loop has finished and all todos-items have been reorganized into their matching arrays, the tableDate array that was initialized above is set to the value of the allSections array (the end result of the for-loop)

tableData = allSections

1 Like

I am trying to apply this collation to my table view (another app) where the table view data source is detached from the view controller and this doesn’t work… I wonder why…

@notationmaster What errors do you get exactly?

We solved this in another thread. Thanks!

This video was just too complex.

Thanks for the feedback!