Created
August 10, 2012 19:05
-
-
Save anonymous/3316942 to your computer and use it in GitHub Desktop.
This file contains 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
#import "ViewController.h" | |
@interface ViewController () <UITableViewDelegate, UITableViewDataSource> | |
@property (weak, nonatomic) IBOutlet UITableView *tableView; | |
@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentedControl; | |
@property (strong, nonatomic) NSArray *first; | |
@property (strong, nonatomic) NSArray *second; | |
@end | |
@implementation ViewController | |
@synthesize tableView = _tableView; | |
@synthesize segmentedControl = _segmentedControl; | |
@synthesize first = _first; | |
@synthesize second = _second; | |
- (NSArray *)first { | |
if (!_first) { | |
_first = [NSArray arrayWithObjects:@"One", @"Two", @"Three", nil]; | |
} | |
return _first; | |
} | |
-(NSArray *)second { | |
if (!_second) { | |
_second = [NSArray arrayWithObjects:@"A", @"B", @"C", nil]; | |
} | |
return _second; | |
} | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
self.tableView.delegate = self; | |
self.tableView.dataSource = self; | |
} | |
- (void)viewDidUnload | |
{ | |
[self setTableView:nil]; | |
[self setSegmentedControl:nil]; | |
[super viewDidUnload]; | |
// Release any retained subviews of the main view. | |
} | |
- (IBAction)valueChanged:(UISegmentedControl *)sender { | |
NSLog(@"%i", sender.selectedSegmentIndex); | |
[self.tableView reloadData]; | |
} | |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation | |
{ | |
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); | |
} | |
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { | |
return 1; | |
} | |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { | |
if (self.segmentedControl.selectedSegmentIndex == 0) { | |
return [self.first count]; | |
} else { | |
return [self.second count]; | |
} | |
return 0; | |
} | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { | |
static NSString *CellIdentifier = @"Cell"; | |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; | |
if (cell == nil) { | |
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; | |
} | |
if (self.segmentedControl.selectedSegmentIndex == 0) { | |
cell.textLabel.text = [self.first objectAtIndex:indexPath.row]; | |
} | |
if (self.segmentedControl.selectedSegmentIndex == 1) { | |
cell.textLabel.text = [self.second objectAtIndex:indexPath.row]; | |
} | |
return cell; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment