我在 NavigationBar 中添加了一个“排序”按钮来对 TableView 进行排序。TableView 是这样构建的:
- (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];
}
这是排序按钮的操作:
- (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];
}
这是捕捉按钮点击的委托方法:
- (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
}
}
如何在 ClickedButtonAtIndex 中实现 SortDescriptor 方法,并更新 TableView?
我希望 tableview 默认按受欢迎程度排序。
Plist结构(字典数组):
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>
必须删除 html 代码的开头才能显示..它看起来像一团糟,所以我猜有人知道如何为我修复它..