0
votes

I've looked in those questions, but couldn't find exactly what I want. UITableView and Another View inside ScrollView

Programmatically force a UIScrollView to stop scrolling, for sharing a table view with multiple data sources

I'm trying to do the following: Inside a UIScrollView, have an UIView with couple of controllers.

Below them, something like a UISegmentedControl. Below it should be the UITableView. The UISegmentedControl will switch the content of the UITableView.

All that would be in UIScrollView.

The thing I want to achive is when I scroll down, I want the UISegmentedControl stay at the top of the view. If I continue to scroll down, only the UITableView content will be scrolled.

Is that possible? Thanks!

2
But why do you need a UIScrollView? What you are describing is a segmented control sitting above a UITableView - without an UIScrollView containing them.matt
But above the segmented control I have a few more controls that I want to be part of the scroll.Oded

2 Answers

1
votes

Assuming I understood what you are asking for, I would do this in the following way:

  1. Use one single UITableView with two sections
  2. Put the "couple of controllers" in the first section of the table. Up to you how (wheather in a single cell or whatsoever). You just have to instruct the UITableViewDataSource to return those controls when it asks for the content of the first section
  3. In the UITableViewDelegate method
    (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    in case the section is 1 , build a view containing the segmented control and return it
  4. Whenever the data source is asked for the content of the second section, check the state of the segmented control and return the proper data
  5. Whenever the segmented control change, catch the event and reload the table view. This way, step 4 will provide for the new data to display in the second sections and the table will change content

The trick here is that the UITableView section headers stick to the top of the table while scrolled, so you will achieve exactly what you are searching for. The first section of the table will scroll away, the segmented controller will stick to the top and the section 2 will keep scrolling underneath it.

0
votes

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;
}