Reputation: 15588
I have a UI that looks similar to the mail app. A table view along the left with a single column of items. When one of the those items on the left is selected, details about that item are shown on the right.
When some event occurs in my app that requires the data in the left table view to be reloaded, the current selection is lost and then the right detail view and left master view get out of sync.
The way i hoped to solve this problem was, when it was time to reload the table data, I would: 1. Save the current selected item 2. reload the table data 3. Handle a delegate method or notification that let me know when the reloading was done. 4. Re-select the proper item by finding it in the new list of items in the table.
Unfortunately I cannot find any way to determine when the table is done reloading. Is there a. any way to figure this out, or b. a more elegant solution to this problem?
thanks.
update: In case my problem was unclear, imagine you are in the mail app and you have some message selected. That summary cell is shown as selected on the left, and the details of the message are shown on the right. Suppose new mail comes in which appear as new cells at the top of the table. How is the message you are currently viewing preserved, and not de-selected?
Upvotes: 3
Views: 1785
Reputation: 16598
You can save selection on tableView(_:shouldSelectRow:)
then select the row right after you create the cell in tableView(_:viewFor:row:)
using selectRowIndexes(_:byExtendingSelection:)
.
It is pretty reliable no matter how / when / how many times you reload the table.
Upvotes: 0
Reputation: 1891
reloadData
is something of a sledgehammer. 10.7 offers a better solution.
Instead of using reloadData, when you have new rows to add, use insertRowsAtIndexes:withAnimation:
. When you have rows to delete, use removeRowsAtIndexes:withAnimation:
. And, of course, if an existing row has changed, there's reloadDataForRowIndexes:columnIndexes
.
These should remember the selection for you (at least, the equivalents on iOS do).
If you can't target 10.7, some of the other suggestions will probably help out. noteNumberOfRowsChanged
may also be helpful but I've not actually used it.
Upvotes: 4