I’ve added a “Sort” button to the NavigationBar to sort the TableView. The TableView is build this way:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *myfile = [[NSBundle mainBundle]
pathForResource:@"Object" ofType:@"plist"];
sortedObjectes = [[NSMutableArray alloc]initWithContentsOfFile:myfile];
NSSortDescriptor * sortDesc = [[NSSortDescriptor alloc] initWithKey:@"Popularity" ascending:YES];
[sortedObjects sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]];
[super viewDidLoad];
}
This is the Action for the sort button:
- (IBAction)SortButton:(id)sender;
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sort by" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Name", @"Country", @"Popularity", nil];
[alert show];
[alert release];
}
And this is the delegate method to catch the button click:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
//Sort by name
}
else if (buttonIndex == 1)
{
//Sort by country
}
else if (buttonIndex == 2)
{
//Sort by popularity
}
}
How do I implement the SortDescriptor method in ClickedButtonAtIndex, and update the TableView?
I want the tableview sorted by popularity by default.
Plist structure (array of dictionaries):
plist version="1.0">
array>
dict>
key>Country /key>
string>Italy /string>
key>Name /key>
string>Fezzudo /string>
key>Popularity /key>
integer>1 /integer>
/dict>
dict>
key>Country /key>
string>Spanin /string>
key>Name /key>
string>Alamos Malbec /string>
key>Popularity /key>
integer>2 /integer>
/dict>
/array>
/plist>
Had to remove start of the html code for it to show..it looks like a mess so I guess someone who knows how will fix it for me..
1 Answer