alex willrock
alex willrock

Reputation: 996

how to scroll uitableview, and section header in the top, visible and scroll to

i have UITableview with multiple sections with style group, and light custom header view after scroll, header not stick in the top, and invisible

i need scroll UITableview and section header stick in the top view and visible, like phonebook

how to make it's ?

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320,50)];
    UILabel *labelHeader = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, 120, 40)];
    labelHeader.text = [[_groups objectAtIndex:section] objectForKey:@"nameExerciseGroup"];
    labelHeader.textColor = [UIColor blackColor];
    [labelHeader setFont:[UIFont systemFontOfSize:20]];

[headerView addSubview:labelHeader];
    return headerView;
}

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

Upvotes: 9

Views: 14828

Answers (1)

UITableViewStyleGrouped = The section headers and footers do not float.

UITableViewStylePlain = Any section headers or footers are displayed as inline separators and float when the table view is scrolled

You just change the UITableViewStyle to UITableViewStylePlain

Then to scroll to particular row and section programatically you could use this below UITableViewMethod method.

//Scroll to First row in 2 section
[yourTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]
                     atScrollPosition:UITableViewScrollPositionTop animated:YES];

enter image description here enter image description here

Apple Documentation

Upvotes: 20

Related Questions