我正在 Xcode 5 中构建一个应用程序,我遇到了一些奇怪的UISegmentedControl
.
首先是我正在构建的应用程序的一些信息:
我正在构建一个应用程序,我希望允许用户在注册公司订购产品。作为一项额外的服务,我想让他们看到他们所做的订单,甚至可以根据他们的状态过滤订单:所有订单、活动订单和已交付订单。订单显示在 UITableView 中,我在UISegmentedControl
标题视图中创建了一个来过滤订单。当 的selectedSegmentIndex
改变时UISegmentedControl
,它执行NSPredicate
过滤数组并仅显示所需的订单。
现在一切正常,除了一件事:UISegmentedControl
当我选择另一个段时,我没有更新它的视图。默认selectedSegmentIndex
是 ' Active
',因为用户可能对活动订单最感兴趣,但是当我将其更改为 'All' 时,表格视图会显示所有订单(因此谓词有效),但视图保持不变selectedSegmentIndex
。
我做了很多研究来解决这个问题,但没有答案能解决我的问题..我的代码:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if(section == 0) {
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f,0.0f, 320, 45)]; // x,y,width,height
//label
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320, 20)];
title.text = @"Kies een filter...";
[headerView addSubview:title];
//segmentedcontrol
NSArray *itemArray = [NSArray arrayWithObjects: @"Alle", @"Actief", @"Afgehandeld", nil];
control = [[UISegmentedControl alloc] initWithItems:itemArray];
[control setFrame:CGRectMake(0.0f, 20.0f, 320.0, 35.0)];
control.userInteractionEnabled = YES;
control.tintColor = [UIColor blackColor];
[control setEnabled:YES];
[control addTarget:self action:@selector(changeFilter:) forControlEvents:UIControlEventAllEvents];
[headerView addSubview:control];
//label containing selected filter info
UILabel *info = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 55.0f, 320, 20)];
if ([selectedFilterInfo isEqualToString:@"Alle"]) {
info.text = @"Alle orders:";
}
else if ([selectedFilterInfo isEqualToString:@"Actief"]) {
info.text = @"Alle actieve orders:";
}
else if ([selectedFilterInfo isEqualToString:@"Afgehandeld"]) {
info.text = @"Alle afgehandelde orders:";
}
[headerView addSubview:info];
headerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bar.png"]];
return headerView;
}
}
以及它触发的动作:
- (void)changeFilter:(id)sender {
UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
if (segmentedControl.selectedSegmentIndex == 0) {
selectedFilterInfo = @"Alle";
filteredArray = nil;
[segmentedControl reloadInputViews];
[segmentedControl setSelectedSegmentIndex:0];
}
else if (segmentedControl.selectedSegmentIndex == 1) {
NSPredicate *deliveredpredicate = [NSPredicate predicateWithFormat:@"SELF.delivered contains[c] %@", @"0"];
NSMutableArray *notDeliveredArray = [NSMutableArray arrayWithArray:[sortedArray filteredArrayUsingPredicate:deliveredpredicate]];
filteredArray = [NSMutableArray arrayWithArray:notDeliveredArray];
selectedFilterInfo = @"Actief";
[segmentedControl reloadInputViews];
[segmentedControl setSelectedSegmentIndex:1];
}
else if (segmentedControl.selectedSegmentIndex == 2) {
NSPredicate *deliveredpredicate = [NSPredicate predicateWithFormat:@"SELF.delivered contains[c] %@", @"1"];
NSArray *deliveredArray = [sortedArray filteredArrayUsingPredicate:deliveredpredicate];
filteredArray = [NSMutableArray arrayWithArray:deliveredArray];
selectedFilterInfo = @"Afgehandeld";
[segmentedControl reloadInputViews];
[segmentedControl setSelectedSegmentIndex:2];
}
[self.tableView reloadData];
}
我没有包括numberOfCellsInSection
等等,因为 tableview 部分工作得很好。唯一不起作用的是更新视图。
任何解决方案将不胜感激,提前谢谢大家!!