开发者

Scrolling a section in UITableView

开发者 https://www.devze.com 2023-01-09 22:22 出处:网络
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 frequent开发者_Go百科ly goes off the screen and the user can scroll

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 frequent开发者_Go百科ly 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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消