【发布时间】:2015-05-30 22:45:29
【问题描述】:
我想使用分段控件对我的表格视图进行排序,并按我的数据核心中的值对其进行排序。根据 Apple 文档,我下面的代码应该可以工作,但它什么也没做。
谁能告诉我我做错了什么?
- (void)viewDidLoad {
[super viewDidLoad];
UISegmentedControl *statFilter = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"A - Z", @"Stock", @"Price", nil]];
[statFilter sizeToFit];
[statFilter addTarget:self action:@selector(MySegmentControlAction:) forControlEvents: UIControlEventValueChanged];
self.navigationItem.titleView = statFilter;
self.artists = [[ArtistDataStore sharedStore] artists];
[self.tableView reloadData];
self.tableView.rowHeight = 60;
}
- (void)MySegmentControlAction:(UISegmentedControl *)segment
{
NSArray *arrayToSort = [[ArtistDataStore sharedStore] artists];
if(segment.selectedSegmentIndex == 0)
{
NSArray * result = [arrayToSort valueForKey:@"name"];
NSSortDescriptor *sd = [[NSSortDescriptor alloc] initWithKey:nil ascending:YES];
result = [result sortedArrayUsingDescriptors:@[sd]];
}
if(segment.selectedSegmentIndex == 1)
{
NSArray * result = [arrayToSort valueForKey:@"stock"];
NSSortDescriptor *sd = [[NSSortDescriptor alloc] initWithKey:nil ascending:YES];
result = [result sortedArrayUsingDescriptors:@[sd]];
}
if(segment.selectedSegmentIndex == 2)
{
NSArray * result = [arrayToSort valueForKey:@"price"];
NSSortDescriptor *sd = [[NSSortDescriptor alloc] initWithKey:nil ascending:YES];
result = [result sortedArrayUsingDescriptors:@[sd]];
}
[self.tableView reloadData];
}
【问题讨论】:
标签: objective-c uitableview uisegmentedcontrol nssortdescriptor