0

我在 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 代码的开头才能显示..它看起来像一团糟,所以我猜有人知道如何为我修复它..

4

2 回答 2

1
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSSortDescriptor *sortDesc;
switch (buttonIndex) {
    case 0:
        return;
        break;
    case 1:
        sortDesc = [[NSSortDescriptor alloc] initWithKey:@"Name" ascending:YES];
        break;
    case 2:
        sortDesc = [[NSSortDescriptor alloc] initWithKey:@"Country" ascending:YES];
        break;
    case 3:
        sortDesc = [NSSortDescriptor sortDescriptorWithKey:@"Popularity" ascending:NO comparator:^(id obj1, id obj2) { return [obj1 compare:obj2 options:NSNumericSearch]; }];
        break;
    }
    [sortedObjects sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]];
    [self.tableView reloadData];
}
于 2012-07-12T02:57:59.170 回答
0

要显示超过 2 个按钮(选项),最好使用 UIACtionSheet 而不是 UIAlertView。操作表对我来说更好,因为我希望弹出菜单中最多有 10 个按钮。

于 2012-07-12T18:12:20.883 回答