//UISegmentedControl with TableViewController
//complete working code
@interface TabTwoScheduleViewController() <UITableViewDelegate , UITableViewDataSource>
{
CGRect rect;
NSArray *list;
NSArray *list1;
NSArray *list2;
NSArray *list3;
NSArray *list4;
UITableView *segmentTableView;
}
@end
@implementation TabTwoScheduleViewController
- (void)viewDidLoad {
[super viewDidLoad];
rect = [[UIScreen mainScreen]bounds];
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 10, rect.size.width-20, rect.size.height/10-23)];
scroll.contentSize = CGSizeMake(rect.size.width, rect.size.height * 2);
scroll.showsHorizontalScrollIndicator = YES;
scroll.backgroundColor = [UIColor yellowColor];
NSArray *itemArray = [NSArray arrayWithObjects: @"ONLINE", @"CLASSROOM", @"WEBCASTING", nil];
list = [NSArray arrayWithObjects:@"list.1",@"list.2",@"list.3",@"list.4",@"list.5", nil];
list1 = [NSArray arrayWithObjects:@"list1.1",@"list1.2",@"list1.3",@"list1.4",@"list1.5", nil];
list2 = [NSArray arrayWithObjects:@"list2.1",@"list2.2",@"list2.3",@"list2.4",@"list2.5", nil];
list3 = [NSArray arrayWithObjects:@"list3.1",@"list3.2",@"list3.3",@"list3.4",@"list3.5", nil];
list4 = [NSArray arrayWithObjects:@"list4.1",@"list4.2",@"list4.3",@"list4.4",@"list4.5", nil];
// segmentedControl is declared as property
self.segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
self.segmentedControl.frame = CGRectMake(0, 0, rect.size.width-20, 50);
self.segmentedControl.segmentedControlStyle =UISegmentedControlStylePlain;
[self.segmentedControl addTarget:self action:@selector(MySegmentControlAction:) forControlEvents: UIControlEventValueChanged];
self.segmentedControl.selectedSegmentIndex = 0;
[scroll addSubview:self.segmentedControl];
[self.view addSubview:scroll];
//ADDING TABLEVIEW OVER VIEW(I added this view to get leading and trailing space for tableViewCell)
UIView *vw = [[UIView alloc]initWithFrame:CGRectMake(0, 70, rect.size.width, rect.size.height)];
vw.backgroundColor = [UIColor redColor];
[self.view addSubview:vw];
//TABLE VIEW
segmentTableView = [[UITableView alloc]initWithFrame:CGRectMake(10, 0, rect.size.width-20, rect.size.height-230) style:UITableViewStylePlain];
segmentTableView.backgroundColor = [UIColor yellowColor];
segmentTableView.delegate = self;
segmentTableView.dataSource = self;
[vw addSubview:segmentTableView];
}
// number of sections for tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView
{
NSInteger a=0;
switch (self.segmentedControl.selectedSegmentIndex)
{
case 0:
a=list.count;
break;
case 1:
a=list1.count;
break;
case 2:
a=list1.count;
break;
default:
a=list1.count;
break;
}
return a;
}
// number of row in the section
- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"ScheduleCustomTableViewCell";
//ScheduleCustomTableViewCell is name of custom TabelViewCell
ScheduleCustomTableViewCell *cell =(ScheduleCustomTableViewCell *) [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ScheduleCustomTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
// conditions to get different values on label
if (self.segmentedControl.selectedSegmentIndex==0)
{
cell.labelAddress1.text = [list objectAtIndex:indexPath.section];
cell.labelAddress2.text = [list1 objectAtIndex:indexPath.section];
}
else if (self.segmentedControl.selectedSegmentIndex==1)
{
cell.labelAddress1.text = [list1 objectAtIndex:indexPath.section];
cell.labelAddress2.text = [list2 objectAtIndex:indexPath.section];
}
else
{
cell.labelAddress1.text = [list2 objectAtIndex:indexPath.section];
cell.labelAddress2.text = [list3 objectAtIndex:indexPath.section];
}
cell.backgroundColor = [UIColor yellowColor];
return cell ;
}
-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
return 130;
}
// Header is given to get spacing between TableViewCells
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] init];
headerView.backgroundColor = [UIColor redColor];
return headerView;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)MySegmentControlAction:(UISegmentedControl *)segment
{
[segmentTableView reloadData];
}
@end
C'est de quoi je parle. merci beaucoup – user567