cwRichardKim
cwRichardKim

Reputation: 1060

ios: is there a function that calls when you change uitable sections?

Basically, I'm making a calendar-ish app using a uitable of events.

The setup is basically a bar on top of a uitableview, where the bar has 7 sections for the 7 days of the week. When you scroll through the events, I want the bar to change to reflect the day that you are looking at. This is kind of like what fantastical does.

Is there a function that gets called when a section header hits the top of the screen?

Thanks!

Upvotes: 0

Views: 112

Answers (2)

cwRichardKim
cwRichardKim

Reputation: 1060

for those who want to do this in the future, this is what I did. Tim's answer didn't work because the zero point is always at the top of the table, even if you scroll. So, I used his "sectionAtTop" idea

NSIndexPath *indexPath = [[self.tableView indexPathsForVisibleRows]objectAtIndex:0];
sectionAtTop = indexPath.section;

this takes the visible first cell and takes the indexpath for it. This worked, but i needed to put it in a place where it would update properly. I ended up placing it in

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

However, this posed another problem because for some reason scrollviewdidscroll was being called before my cells were being populated, so it ended up crashing the app (no object at index 0)

So I created a boolean called "tableDidLoad" and set it to NO. Then, at the bottom of the code where the cells finish populating, i set the boolean tableDidLoad to true. So, my code looked like

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (tableDidLoad ) {
        NSIndexPath *indexPath = [[self.tableView indexPathsForVisibleRows]objectAtIndex:0];
        sectionAtTop = indexPath.section;
        [self changeDOW];
    }
}

where changeDOW is the function called with a switch statement that does the desired method call.

Upvotes: 1

Timothy Moose
Timothy Moose

Reputation: 9915

Lets say you have a property called sectionAtTop, then something like this should work:

- (void)setSectionAtTop:(NSInteger)sectionAtTop
{
    if (_sectionAtTop != sectionAtTop) {
        _sectionAtTop = sectionAtTop;
        // update the top bar here with the new section number
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:CGPointZero];
    self.sectionAtTop = indexPath.section;
}

Upvotes: 1

Related Questions