I've implemented a UITableView with 2 sections. The first section has no header and the header's height is 0.
The first and only cell of the first section is the UIView that I want to be scrollable (Red background). The second section's header is the UISegmentedControl (Green background).
- (void)viewDidLoad {
[super viewDidLoad];
data = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", @"6",@"7", @"8", @"9",@"10",nil];
table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
table.delegate = self;
table.dataSource = self;
[self.view addSubview:table];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
//Return zero for the first section's header.
if (section == 0)
return 0;
return 20;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
//Return nil for the first section's header view.
if (section == 0) return nil;
head1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 20)];
head1.backgroundColor = [UIColor greenColor];
return head1;
}
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0) return 1;
return data.count;
}
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//First section - put the UIView that will be scrollable
if (indexPath.section == 0)
{
static NSString *CellIdentifier = @"ControlCell1";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
head = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
head.backgroundColor = [UIColor redColor];
[cell.contentView addSubview:head];
return cell;
}
static NSString *CellIdentifier = @"ControlCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [data objectAtIndex:indexPath.row];
return cell;
}