How to connect a table view to a sorted data source

I have a Table View Controller that displays a list of posts. I want the user to be able to choose different sorting options for their posts. Some possible sorting options might be most recent, read / unread, by sender, etc.

What I am discussing in my own mind is the correct way to manage the underlying message list. Here's my current plan:

I have my own MessageList model object. I'll add a sortedBy property so that any Controller can check the current sort method and resort to the list if needed. The MessageList should be responsible for properly nesting new messages based on the sorting method chosen.

I like this approach because I don't need to make a sorted copy of the MessageList. It also makes things easier for the controller (indexPath.row == indexOfMessageInList).

Has anyone been in a similar situation and found a better approach?

0


a source to share


1 answer


The plan looks pretty solid, what I would do is implement a custom setter for sortedBy, and whenever it is called, the call is also called [tableView reloadData]

whenever an actual change occurs. Something like that:



- (void) setSortedBy:(SEL)someSortMethod {
  # check if the sort method is new or not
  # perform operations on list to sort it
  sortedBy = someSortMethod;

  [self.tableView reloadData];
}

      

0


a source







All Articles