user3167216
user3167216

Reputation: 3

iOS: How to tell if you scroll past the a certain row in a UITableView?

I need a way to detect if I scroll past the 5th row in a UITableView. Once I past the 5th row I want to call a method. I have no idea how to do this or where to begin. I will post the UITableView code. If anyone can help me I will deeply appreciate it.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
   {
ListingCell  *cell ;
if (cell == nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ListingCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}

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

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 20;
}

Upvotes: 0

Views: 142

Answers (2)

Christian Schnorr
Christian Schnorr

Reputation: 10786

Try the following:

- (void)scrollViewDidScroll:(UITableView *)tableView
{
    CGRect row = [tableView rectForRowAtIndexPath:[NSIndexPath indexPathForRow:4 inSection:0];
    if (tableView.contentOffset.y > CGRectGetMaxY(row)) {
        // you did it
    }
}

Or CGRectGetMinY, depending on what exactly you're trying to achieve.

Upvotes: 0

Putz1103
Putz1103

Reputation: 6211

All you have to do is implement the UIScrollViewDelegate method -scrollViewDidScroll. Then you can manually calculate anything you want from the current UITableView contentOffset property.

-(void)scrollViewDidScroll:(UIScrollView *)inScrollView
{
    int currentOffsetY = inScrollView.contentOffset.y;

    //Now use that y position to figure out if you have scrolled past where you want.
}

You may also want a boolean so only call the "past here" method once.

Upvotes: 4

Related Questions