views:

37

answers:

2

I have an instance of UITableView with two sections, the first with one row and the second with up to eight rows. The second section frequently goes off the screen and the user can scroll the whole table view down to see these at will.

However I would like the first section to always remain in view because its information is useful. I'd like only the second section to be scrollable, the first section remaining stationary and in view.

What do you suggest?

A: 

I'm afraid that you can't do this with a single table. All the sections will be scrolled. However, if the first section always contains one row and you want to keep it in place, then why should you keep it in the table? You can move this out of table.

taskinoor
+1  A: 

You cannot scroll each section separately. However according to your description is sounds like your first section isn't really a section and should actually be an header for the second section. When you create an header for a section, the header will always remain visible. To create a custom view section you need to implement viewForHeaderInSection and heightForHeaderInSection defined in the UITableViewDelegate protocol.

Here's an example

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
      return 60;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
      UIView *myHeader = [[[UIView alloc] initWithFrame:CGRectMake(0,0,60,320)] autorelease];
      myHeader.backgroundColor = [UIColor redColor];
      UILabel *myLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,30,150)] autorelease];
      myLabel.text = @"Testing header view";
      [myHeader addSubView:myLabel];
      return myHeader;

}

This way you can create a table view with one section and an header for that section that will remain in view even when the table is scrolled.

Ron Srebro
BTW I hope my code works, I wrote it directly here and not in xcode.
Ron Srebro
@Ron: OK, make the top section part of the second section's header... That might have fringe benefits. Thanks for the great post!
SpecialK
@SpecialK you welcome. How about accepting the answer then?
Ron Srebro