2

In a plain UITableView with custom UIView's as section headers, is there a way to calculate:

  • When one of the Section is on the top, the distance between that section and the next one that would come?

I am expecting to calculate this here:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
Rui Peres
  • 25,741
  • 9
  • 87
  • 137
  • try numOfCellsInSection * heightOfCell – Durgaprasad May 28 '13 at 11:14
  • what you want to do? scroll to next section on some event? – Durgaprasad May 28 '13 at 11:24
  • If the sections are close to each other (for a given space, 10p for example) I want to do something on the top section header. – Rui Peres May 28 '13 at 11:25
  • @MikePollard I was expecting this to work `headerViewForSection` but the header view comes always as `nil`. – Rui Peres May 28 '13 at 11:40
  • 1
    @JackyBoy How about storing the custom section header views in an array and checking for the section view's frame with respect to contentOffset of the table view each time it scrolled? That way you will have access to the section header views all time. This link shows how you can get the [visible section header](http://stackoverflow.com/a/4253124/1407017). Based on this you can calculate which is the next section view going to be visible and also the distance in px. – Amar May 28 '13 at 13:14
  • I am doing that right now. But I don't like the approach of storing that in an array. – Rui Peres May 28 '13 at 13:18

3 Answers3

2

You can find the number of rows in that section by calling the tableView:numberOfRowsInSection: method from the UITableViewDataSourceDelegate protocol. You can get the height for each row in the section with the tableView:heightForRowAtIndexPath: method from the UITableViewDelegate protocol. Add up the height for all the rows and you have the distance you want.

Your code would look something like this, assuming you have a reference to the tableview and the section.

float totalHeight = 0;

for (int i = 0; i < [tableViewDataSourceDelegate 
                                       tableView:tableView 
                           numberOfRowsInSection:section]; i ++) {
    totalHeight += [tableViewDelegate 
                            tableView:tableView 
              heightForRowAtIndexPath:[NSIndexPath 
                                   indexPathForRow:i 
                                         inSection:section]];
}

Haven't had a chance to test this code, but it Should Work[tm].

Edit

This will only work if the header is at the top.

Maarten
  • 1,873
  • 1
  • 13
  • 16
  • You're forgetting the fact that the section header 'sticks' at the top when you scroll through a section so you need to take into account current position within the upper most visible section too. – Mike Pollard May 28 '13 at 11:20
  • Doesn't work. A Section header can only be showing 2 cells out of 3, remember that on a plain `UITableView` while the section has cells to show, it sticks to the top... – Rui Peres May 28 '13 at 11:20
  • Admit I hadn't taken that into account. – Maarten May 28 '13 at 11:29
  • Maarten, it doesn't matter what code you put, since the theory behind is wrong. – Rui Peres May 28 '13 at 11:29
0

(assuming all rows are same height)

NSIndexPath *topCellIndexPath = [tableView indexPathsForVisibleRows][0];
UITableViewCell *topCell = [tableView cellForRowAtIndexPath: topCellIndexPath];
CGFloat distanceToNextSection = [tableView convertRect: [topCell frame] fromView: topCell.superview].origin.y - tableView.contentOffset.y + ([self tableView: tableView numberOfRowsInSection: topCellIndexPath.section] - topCellIndexPath.row)*tableView.rowHeight
maroux
  • 3,764
  • 3
  • 23
  • 32
0

I was able to solve this by doing the following:

  • Before creating an section header, check if you have the section header for a given section. If you do return it from the NSMutableArray. If not keep going.
  • When you create the section header, keep a reference to it in a NSMutableArray.

  • When you scroll in:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView

Do the following:

// Get the toppest section
NSUInteger sectionNumber = [[self.tableView indexPathForCell:[[self.tableView visibleCells] objectAtIndex: 0]] section];

// Get a reference to it
SFBasicSectionHeader *topHeader = [arrayOfWeakHeaders objectAtIndex:sectionNumber];

SFBasicSectionHeader *bellowHeader;
// Check if it's Ok to get the bellow header
if (sectionNumber+1<[arrayOfWeakHeaders count] && [arrayOfWeakHeaders objectAtIndex:sectionNumber +1])
{
    bellowHeader = [arrayOfWeakHeaders objectAtIndex:sectionNumber+1];
}

The difference between both will be:

CGFloat differenceBetweenTopAndBellowSection = bellowHeader.frame.origin.y - topHeader.frame.size.height - self.tableView.contentOffset.y;

Done.

Rui Peres
  • 25,741
  • 9
  • 87
  • 137