Detecting if a UITableView section header is floating

Is there a way to determine if a section title in a UITableView current? I want to scroll the table view to the position of the header only if it is floating.

Thanks in advance!

+4
source share
5 answers

The title will be floating if the first cell of the section is no longer visible. So:

 NSIndexPath *topCellPath = [[self.tableView indexPathsForVisibleRows] objectAtIndex:0]; if (topCellPath.row != 0) { // Header must be floating! } 

You can achieve a similar effect by scrollToRowAtIndexPath:atScrollPosition:animated: index path with scrollToRowAtIndexPath:atScrollPosition:animated: and the scroll position of UITableViewScrollPositionNone - this will not scroll if the first cell in the section was already on the screen.

+8
source

You can first save the original rectangle of the section title.

  headerView.originalPoint = [tableView rectForHeaderInSection:section].origin; 

And send the message scrollViewDidScroll:scrollView to the header.

In the title view

 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { BOOL isFloating = self.frame.origin.y > self.originalPoint.y; if (isFloating) { //DO what you want } } 
+1
source

It works.

 @implementation UITableView (rrn_extensions) -(BOOL)rrn_isFloatingSectionHeaderView:(UITableViewHeaderFooterView *)view { NSNumber *section = [self rrn_sectionForHeaderFooterView:view]; return [self rrn_isFloatingHeaderInSection:section.integerValue]; } -(BOOL)rrn_isFloatingHeaderInSection:(NSInteger)section { CGRect frame = [self rectForHeaderInSection:section]; CGFloat y = self.contentInset.top + self.contentOffset.y; return y > frame.origin.y; } -(NSNumber *)rrn_sectionForHeaderFooterView:(UITableViewHeaderFooterView *)view { for (NSInteger i = 0; i < [self numberOfSections]; i++) { CGPoint a = [self convertPoint:CGPointZero fromView:[self headerViewForSection:i]]; CGPoint b = [self convertPoint:CGPointZero fromView:view]; if (ay == by) { return @(i); } } return nil; } @end 
+1
source

The code works for me.

 - (BOOL)isSection0HeaderSticky { CGRect originalFrame = [self.listTableView rectForHeaderInSection:0]; UIView *section0 = [self.listTableView headerViewForSection:0]; if (originalFrame.origin.y < section0.frame.origin.y) { return YES; } return NO; } 
0
source

Adding @robdashnash quick response port

 extension UITableView { func isFloatingSectionHeader( view:UITableViewHeaderFooterView )->Bool { if let section = section( for:view ) { return isFloatingHeaderInSection( section:section ) } return false } func isFloatingHeaderInSection( section:Int )->Bool { let frame = rectForHeader( inSection:section ) let y = contentInset.top + contentOffset.y return y > frame.origin.y } func section( for view:UITableViewHeaderFooterView )->Int? { for i in stride( from:0, to:numberOfSections, by:1 ) { let a = convert( CGPoint.zero, from:headerView( forSection:i ) ) let b = convert( CGPoint.zero, from:view ) if ay == by { return i } } return nil } } 
0
source

All Articles