Skip to content

Instantly share code, notes, and snippets.

Created August 10, 2012 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3316942 to your computer and use it in GitHub Desktop.
Save anonymous/3316942 to your computer and use it in GitHub Desktop.
#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