1

I'm working with a UITableView. The table view has a HeaderView with a UIImageView that changes its height when the user scroll the table view.

In addition to this I also have a custom uiview for the section of the tableview .. My problem is that after setting the content Inset for the tableview, the section does not follow the scrolling of the table ..


Before Scroll

enter image description here

After Scroll

enter image description here

Surely it is a problem of content Inset, I did various tests but I could not find a solution ...

I would like the section of the tableView to follow the scroll and stop when it gets under the status bar (about 25 points of self.view.frame.origin.y)

Which path should I follow to solve my problem?

This is the code I am using


Register Xib for section header

- (void)viewDidLoad {
    [super viewDidLoad];

    _tableView.delegate =  self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];

    [_tableView registerNib:[UINib nibWithNibName:@"DashboardTopHeader" bundle:nil] forHeaderFooterViewReuseIdentifier:dashboardTopHeader];

    [self setupHeaderView];
}

Generate Section Header

#pragma mark - Dashboard Top Header
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    DashboardTopHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:dashboardTopHeader];
    return header;
}

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

Setup the Header View of TableView With Photo

-(void)setupHeaderView {
    _headerView = [[UIView alloc] init];
    _headerView = self.tableView.tableHeaderView;
    _tableView.tableHeaderView = nil;
    [_tableView addSubview:_headerView];

    // 130 is Height of headerView
    CGFloat newHeight = 130 - 50 /2; 

    _tableView.contentOffset = CGPointMake(0, -newHeight);
    _tableView.contentInset = UIEdgeInsetsMake(newHeight, 0, 0, 0 );

    [self updateHeaderView];
}

-(void)updateHeaderView {
    CGFloat newHeight = 130 - 50 /2;
    CGRect headerFrame = CGRectMake(0, -newHeight, self.tableView.frame.size.width, 130);

    if (self.tableView.contentOffset.y < newHeight) {
        headerFrame.origin.y = self.tableView.contentOffset.y;
        headerFrame.size.height = - self.tableView.contentOffset.y + 50/2;
    }

    _headerView.frame = headerFrame;
}

Use TableView Delegate for scroll Content

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self updateHeaderView];
}
kAiN
  • 2,559
  • 1
  • 26
  • 54
  • out of your images is red patch a section header or table header? – Van Mar 15 '18 at 12:50
  • redView is Section.. the Photo is Header View – kAiN Mar 15 '18 at 12:51
  • I am unable to capture your expected result. but as per my understanding by default the feature is there if its 0th section i.e first section of table. You no need to set the header view explicitly as you did with [_tableView addSubview:_headerView];, instead set it default tableHeader of table: _tableView.tableHeaderView = _headerView;... try this first – Van Mar 15 '18 at 13:05

0 Answers0