Group Group Group Group Group Group Group Group Group

Beginning Table Views · Sectioning by Priority | Ray Wenderlich

#1

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

#2

Hi! Could you please, please, please not do video transition like from 2:55 to 3:00. It’s very distracting and confusing. On 2:55 we have two errors:
22

And on 3:00 we’re fixing a completely different error and previous errors are magically fixed:
07

Maybe it’s a good idea to add annotation about this under the video.

0 Likes

#3

Looks like a bug during editing. We’ll take look. Thanks for the heads up!

1 Like

#4

Hi! Actually, looks like there is one issue in deleteItems function: you loop through indexes of items that should be deleted, but when you delete items consistently your model will crush, cause models indexes and indexes in array through you loop become unsynchronized.
UPD: Ooops! Didn’t see that you actually fixed it, though there is still bug. I mean even if you fix out of bound error you still may delete another item.
For example:
[‘Do app’, ‘Wake up’, ‘Go to sleep’, ‘Study design pattern’].
When you select first ‘Do app’ and then select ‘Go to sleep’ he will take array[0, 2]. And when you delete ‘Do app’, next item that will be deleted is ‘Study design pattern’, cause now he contained in todos[2].
Easy way to fix this:

@IBAction func deleteItems(_ sender: Any) {
    var was: [Int] = []
    if let selectedRows = tableView.indexPathsForSelectedRows {
        for indexPath in selectedRows {
            print(indexPath.row)
            var actualPath = indexPath
            for i in was {
                if i < indexPath.row {
                    actualPath.row -= 1
                }
            }
            was.append(indexPath.row)
            if let priority = priorityForSectionIndex(indexPath.section) {
                let todos = todoList.todoList(for: priority)
                print(todos[actualPath.row].text)
                let item = todos[actualPath.row]
                todoList.remove(item, from: priority, at: actualPath.row)
            }
        }

Minor changes, but app will work properly.

0 Likes

#5

Thanks for the heads up - we’ll get this fixed in the next iteration.

0 Likes

#6

I gave up on following along in Xcode at this point, because following just the changes in the video isn’t enough to get the app to work again.

0 Likes

#7

@dschuler Please let us know what errors you get exactly when you get a chance. Thank you!

0 Likes

#8

Tapping ‘add’ errors out in the debugger - the backing array and table view get out of sync. There were a couple of other bugs in previous lessons that actually ended up being informative from a learning perspective, but this tutorial should probably be re-worked for errors.

0 Likes

#9

@dschuler Thank you for your feedback - much appreciated! We will fix everything soon. Thanks again!

0 Likes

#10

I think I’ve came up with an even simpler solution: just sort and iterate in reverse order like this:

@IBAction func deleteItems(_ sender: UIBarButtonItem) {
    if let selectedRows = tableView.indexPathsForSelectedRows?.sorted() {
        for i in stride(from: selectedRows.count - 1, through: 0, by: -1) {
            let indexPath = selectedRows[i]
            
            if let priority = priorityForSectionIndex(indexPath.section) {
                let todos = todoList.todoList(for: priority)
                let item = todos[indexPath.row]
                
                todoList.remove(item, from: priority, at: indexPath.row)
            }
        }
        
        tableView.beginUpdates()
        tableView.deleteRows(at: selectedRows, with: .automatic)
        tableView.endUpdates()
    }
}
0 Likes

#11

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

0 Likes

#12

Really bad editing at 2.55 - 3 min mark as others have said, this has happens several times in the series where errors and code changes are quickly and quietly done, you need hawk eyes to catch.

If you are getting the error “Variable used within its own initial value” you need to change “indexPath” to “IndexPath”

0 Likes

#13

@idesignpixels Thank you for the heads up! We will definitely fix these in the next edition.

0 Likes