Created
October 15, 2014 03:11
-
-
Save paulw11/5193ba2343d39326fe3e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
<plist version="1.0"> | |
<array> | |
<dict> | |
<key>size</key> | |
<string>26</string> | |
<key>name</key> | |
<string>5</string> | |
<key>ph</key> | |
<string>1</string> | |
</dict> | |
<dict> | |
<key>size</key> | |
<string>26</string> | |
<key>name</key> | |
<string>4</string> | |
<key>ph</key> | |
<string>1</string> | |
</dict> | |
<dict> | |
<key>size</key> | |
<string>18</string> | |
<key>name</key> | |
<string>3</string> | |
<key>ph</key> | |
<string>3</string> | |
</dict> | |
<dict> | |
<key>size</key> | |
<string>24</string> | |
<key>ph</key> | |
<string>1</string> | |
<key>name</key> | |
<string>2</string> | |
</dict> | |
<dict> | |
<key>size</key> | |
<string>18</string> | |
<key>ph</key> | |
<string>1</string> | |
<key>name</key> | |
<string>1</string> | |
</dict> | |
</array> | |
</plist> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.m | |
// segmentTest | |
// | |
// Created by Paul Wilkinson on 15/10/2014. | |
// Copyright (c) 2014 Paul Wilkinson. All rights reserved. | |
// | |
#import "ViewController.h" | |
@interface ViewController () | |
@property (nonatomic,strong) NSArray *myArrayOfDictionaries; | |
@property (nonatomic,strong) NSArray *filteredArray; | |
@property (nonatomic,strong) NSMutableArray *predicates; | |
@property (nonatomic,strong) NSArray *predicateKeys; | |
@property (weak,nonatomic) IBOutlet UITableView *tableview; | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view, typically from a nib. | |
NSString *plistPath=[[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]; | |
self.myArrayOfDictionaries=[NSArray arrayWithContentsOfFile:plistPath]; | |
self.predicateKeys=@[@"size",@"ph"]; | |
self.predicates=[NSMutableArray new]; | |
for (int i=0;i<self.predicateKeys.count;i++) { | |
[self.predicates addObject:[NSNull null]]; | |
} | |
[self setupFilteredArray]; | |
} | |
- (void)didReceiveMemoryWarning { | |
[super didReceiveMemoryWarning]; | |
// Dispose of any resources that can be recreated. | |
} | |
#pragma mark - filtering | |
-(void) setupFilteredArray { | |
NSArray *tempArray=[NSArray arrayWithArray:self.myArrayOfDictionaries]; | |
for (int i=0;i<self.predicates.count; i++) { | |
NSPredicate *pred=self.predicates[i]; | |
if (![[NSNull null] isEqual:pred]) { | |
tempArray=[tempArray filteredArrayUsingPredicate:pred]; | |
} | |
} | |
self.filteredArray=tempArray; | |
[self.tableview reloadData]; | |
} | |
-(IBAction)segmentChanged:(UISegmentedControl *)sender | |
{ | |
NSPredicate *pred; | |
NSUInteger controlTag=sender.tag; | |
NSString *segmentValue=[sender titleForSegmentAtIndex:sender.selectedSegmentIndex]; | |
if ([segmentValue isEqualToString:@"-"]) { | |
pred=(NSPredicate *)[NSNull null]; | |
} | |
else { | |
pred=[NSPredicate predicateWithFormat:@"(%K == %@)",self.predicateKeys[controlTag],segmentValue]; | |
} | |
self.predicates[controlTag]=pred; | |
[self setupFilteredArray]; | |
} | |
#pragma mark - UITableView Data source | |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { | |
return self.filteredArray.count; | |
} | |
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
static NSString *cellIdentifier=@"Cell"; | |
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; | |
NSDictionary *dict=(NSDictionary *)self.filteredArray[indexPath.row]; | |
cell.textLabel.text=dict[@"name"]; | |
return cell; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment