Group Group Group Group Group Group Group Group Group

Beginning Table Views · Adding Checklist Item | Ray Wenderlich

#1

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

If you had an app where you always inserted a single item at the top (e.g. new message arrivals where most recent one is shown at top), is there a benefit in calling tableview.insertRows vs tableview.reloadData? Regardless of which method you use, it would result in having to repaint every single row on the screen anyway because new item is inserted at top and everything else has to be pushed down, so wondering if there is a benefit in calling insertRows vs reloadData.

#3

If you reload, it will ask for a cell for every row that will be displayed, and rebuild the table view. It is fast, so it is not really a performance problem. If you just insert a row, though, the only row it builds is the new one. It doesn’t have to rebuild the other rows, it just moves them down in the display. That’s more efficient, but it would be hard to detect the difference. There are cases where you might want to preserve the other rows, for instance if one of them is currently selected and you don’t want to lose that.

#4

insertRows has the benefit of providing an animation to the end user. They get to see a visual effect whereas a reload will just update the screen without any indication that something has changed. I use insertRow when the user initiates an action that creates a new row and times when a user is returning to the screen - say from navigating a navigation controller - then I’ll just reload the data.

#5

Hi Brian,

It seems to me that if you change the order of " _ = todoList.newTodo()" and “let newRowIndex = todoList.todos.count” the app crashes. Isn’t it more logical to count the todoList.todos array after we add newTodo list?

When you define ‘let newRowIndex = todoList.todos.count’ it the number(Int) does not take account for the newly created list??

#6

Short answer: 0-based indexing.
If the todo list has 5 items, their indexes will be 0 to 4. The count is always 1 more than the highest index. That means that the count is the index value for the next new item that gets added.

1 Like
#7

Where did you reference the tableView ? I had to create an IBOutlet and can’t find it in your code ?

#8

TableViewControllers come with the table view hooked up. If you are manually adding your table, then you’ll need to add an IBOutlet. Cheers!