Tuesday, March 13, 2012

Determine end of UIScrollView

Today their are lots of clients who are requesting to append data at the end of the last record of the table view by making call to the web server.


So i just googled my way out to come up with a working solution for this. 


Step 1: Open xcode and create a table view which has all the font name in it, given below is the output for the same.






Step 2: Add the delegate method of the scroll view which detects end of dragging


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate


Step 3: Create a function that will detect the end of scroll view present inside the table view, here we have to compare the scrolling y-axis with the tables height 



- (BOOL)detectEndofScroll{

    BOOL scrollResult;
    CGPoint offset = self.tableView.contentOffset;
    CGRect bounds = self.tableView.bounds;
    CGSize size = self.tableView.contentSize;
    UIEdgeInsets inset = self.tableView.contentInset;
    float yaxis = offset.y + bounds.size.height - inset.bottom;
    float h = size.height;
    if(yaxis > h) {
        scrollResult = YES;
    }else{
        scrollResult = NO;
    }
    
    return scrollResult;
}



Step 4: Use this code in the scroll view delegate method



#pragma mark scroll view delegate

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if ([self detectEndofScroll]){
        UIAlertView *displayMessage = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Reached end of table scroll" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
        [displayMessage show];
    }
}

Step 5: And finally when everything is done load this view inside the iPhone window and run the application.






Wait don't copy paste the code you can download the code from here.

Happy iCoding and have a great day.

2 comments: