Is there any way to find how much UITableView has been scrolled in any direction ? I am interested in amount not in direction.
Asked
Active
Viewed 2,234 times
1
-
Can you please provide more info. Like in which sense you require this, what is the actual requirement, what are the ways you have tried? – Mrunal Sep 06 '13 at 09:44
-
I am getting data from the server and displaying that data into my tableviewcell. but i want to load only 10 records at a time. for this purpose i am using limit-offset thing. but i'm not very much clear how to do this in ios. I am setting those limit-offset in NSUrl. – Uniruddh Sep 06 '13 at 10:18
-
Similar questions: 1. http://stackoverflow.com/questions/5137943/how-to-know-when-uitableview-did-scroll-to-bottom-in-iphone 2. http://stackoverflow.com/questions/9786774/ios-dynamically-load-new-cell-to-the-bottom-of-a-tableview – Mrunal Sep 06 '13 at 10:24
3 Answers
2
You can easily grab the exact offset of the table view by looking at its contentOffset property. For the vertical scroll, look at:
tableView.contentOffset.y;
and with this you can take your tableview to any particular location
[theTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:savedScrollPosition inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
CGPoint point = theTableView.contentOffset;
point .y -= theTableView.rowHeight;
theTableView.contentOffset = point;
for you requirement of load more cells after 10 you can use this logic
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
CGPoint offset = aScrollView.contentOffset;
CGRect bounds = aScrollView.bounds;
CGSize size = aScrollView.contentSize;
UIEdgeInsets inset = aScrollView.contentInset;
float y = offset.y + bounds.size.height - inset.bottom;
float h = size.height;
// NSLog(@"offset: %f", offset.y);
// NSLog(@"content.height: %f", size.height);
// NSLog(@"bounds.height: %f", bounds.size.height);
// NSLog(@"inset.top: %f", inset.top);
// NSLog(@"inset.bottom: %f", inset.bottom);
// NSLog(@"pos: %f of %f", y, h);
float reload_distance = 10;
if(y > h + reload_distance) {
NSLog(@"load more rows");
}
}

D-eptdeveloper
- 2,430
- 1
- 16
- 30
-
Thanks this worked for me. Just one little help. As i load new data,size of tableView will increase. So how can i adjust the height accordingly ? So data doesn't get loaded if not required. – Uniruddh Sep 06 '13 at 12:05
-
you need to set tableview frame according to it's content height that might give you the solution. – D-eptdeveloper Sep 06 '13 at 12:17
1
You need to measure the contentOffset
of the UITableView
when dragging begins and when it ends. Take a difference between the two, it will give you the amount of change from initial to final position.
CGPoint oldOffset;
CGPoint newOffset;
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
oldOffset = scrollView.contentOffset;
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
newOffset = *targetContentOffset;
CGPoint diff = {newOffset.x - oldOffset.x, newOffset.y - oldOffset.y};
// Where diff.x => amount of change in x-coord of offset
// diff.y => amount of change in y coord of offset
}
Hope that helps!

Amar
- 13,202
- 7
- 53
- 71
0
you can find it using below syntax...
NSLog(@"scrolled:%f",yourtableview.contentOffset.y);//y for vertical, x for horizontal

NiravPatel
- 3,260
- 2
- 21
- 31