Group Group Group Group Group Group Group Group Group

Beginning Table Views · Sectioning by Priority | Ray Wenderlich


This is a companion discussion topic for the original entry at


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:

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

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


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


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 {
            var actualPath = indexPath
            for i in was {
                if i < indexPath.row {
                    actualPath.row -= 1
            if let priority = priorityForSectionIndex(indexPath.section) {
                let todos = todoList.todoList(for: priority)
                let item = todos[actualPath.row]
                todoList.remove(item, from: priority, at: actualPath.row)

Minor changes, but app will work properly.


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


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.


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


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.


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


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.deleteRows(at: selectedRows, with: .automatic)


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