views:

681

answers:

3

I am in the process of designing and coding an iPhone application to display movie times. I currently have a Movie Class which contains:

  • NSString title
  • NSSDate releaseDate
  • NSSDate score

My UI is a simple UINavigationController which has a segmented control as it's title and UITableView to display the Movies setup in Interface Builder. The segmented control has 3 segments: Title, Opening Date, and Score (on RottenTomatoes). The data for the table view is currently provided by a NSMutableArray movies which is a property of the AppDelegate.

I know that I can register target actions using the following code, but I am unsure of how to sort the UITableView data at this point:

[segmentedControl addTarget:self action:@selector(action:) forControlEvents:UIControlEventValueChanged];

How and where do I resort the UITableView whenever the UISegmentedControl selected segment changes?

+1  A: 

You can put some logic in the data source's tableView:cellForRowAtPath: method to check the current value of the UISegmentedControl and change the cell returned for each row as appropriate. Then, whenever the value of the segmented control changes, all you have to do is call

[tableView reloadData];

and you're all set.

Tim
A: 

I have a segment control that can order/filter items in three different ways - because the number of items is different depending on the filter / order I use 3 different NSArray objects to hold the sorted lists.

So when I load up the data I put a version of it in the correct place in each array (or not in the array depending on the filter).

I have a call to get the array that uses the state of the segmented control to decide which array to return.

The method that calls an array is used in place of a normal array and this means all I have to do to change things when a segmented control is changed is call [tableView reloadData] and all is done for me.

Grouchal
A: 

Also, you can sort the data with any of the sortArrayUsingxxx methods in NSArray.

Marco Mustapic