I want to do pagination when user scroll tableView
to top, for that i have used this UIScrollView
method. but it will call api multiple time
Asked
Active
Viewed 1,366 times
2

Pankil
- 635
- 6
- 20
-
top means when your tableview displayed last cell ? – KKRocks Jun 23 '17 at 05:41
-
no, first time i will get 10 records, when i scroll to top for displaying old comments. – Pankil Jun 23 '17 at 05:42
-
@KKRocks, can you suggest me to avoid repeat data insertion in array. – Pankil Jun 23 '17 at 05:44
-
repeat means do you have called same api for multiple times ? – KKRocks Jun 23 '17 at 05:45
-
yes it will call multiple times when i scroll tableview. – Pankil Jun 23 '17 at 05:48
2 Answers
2
ry this
When your server gives number of count for data
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 0) { // indexpath.row == 0 means top cell in tableViewCell
if (arrNews.count < totalPageCount ) { //totalPageCount means total number of data exist in server which is needs in server resopnse
[self getDataFromServer];
}
}
}
When your server not gives number of count for data
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 0) { // indexpath.row == 0 means top cell in tableViewCell
if (isMoreData) { //isMoreData is bolean you need to disable while not data found from server
[self getDataFromServer];
}
}
}
-
-
i have explained that its count of server data that represents how money data remain in server . – KKRocks Jun 23 '17 at 05:54
-
from server response, i am getting bunch of 10 record when i call api, i am not getting how many remains data are there? and every time when tableview at top i again call api to get new records like whatsapp. – Pankil Jun 23 '17 at 05:56
-
you needs that number count because how to we know that server have not more data exist . – KKRocks Jun 23 '17 at 05:58
-
i pass one param page_id , it will increase when i got response and when i call api again it will call with increase page_id so if data is not available than it will not return. – Pankil Jun 23 '17 at 06:00
-
when i get comments response, i want to scroll tableview to bottom & for that i will use this [self.tblVWComments setContentOffset:CGPointMake(0, CGFLOAT_MAX)];[self.tblVWComments reloadData]; but it will not scroll to bottom of tableview. – Pankil Jun 23 '17 at 06:26
-
[self.tblVWComments reloadData] call before [self.tblVWComments setContentOffset:CGPointMake(0, CGFLOAT_MAX)]; – KKRocks Jun 23 '17 at 06:30
-
if i write reload method above this [self.tblVWComments setContentOffset:CGPointMake(0, CGFLOAT_MAX)]; first time 10 data is loaded when new data added and this code call again than no comments are displayed. – Pankil Jun 23 '17 at 06:42
-
try to change your line my code : [self. tblVWComments scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.messages.count-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES]; – KKRocks Jun 23 '17 at 06:46
-
actually i also write this line [self.tblVWComments setContentOffset:CGPointMake(0, CGFLOAT_MAX)]; above . – Pankil Jun 23 '17 at 06:50
-
read comment about your scroll at bottom line : https://stackoverflow.com/a/2770189/3901620 – KKRocks Jun 23 '17 at 06:51
-
-
-
i am facing problem with this method, i think this method will not use for pagination. For Example when API call 1st time, i got 10 records, so bool variable os true that more data is available. this method calls everytime when each cell i s gone out of tableview bound so api continueously calling so there's repeating data issue. – Pankil Jun 26 '17 at 04:47
-
-
as i told you that i didn't get number of total comments count, i just pass page_id and based on that i receive next comments. – Pankil Jun 26 '17 at 04:55
-
-
i know the total number of comments, can i use your 1st answer with static number of comments? – Pankil Jun 26 '17 at 05:07
0
You can check it with contentOffset in scrollViewDidScroll
If content offset y position is <=20 and you don't reach the last item call your load more service (pagination records)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView.contentOffset.y <= 20 && !reachedEndOfItems) {
currentOffset = self.wallProfileTableView.contentOffset;
[self loadMoreMessage];
}
}
It will be similar as we load the chats on scrolling down

Kuldeep
- 4,466
- 8
- 32
- 59

Tarun Seera
- 4,212
- 4
- 27
- 41
-
-
its a flag when you are loaded all items like if you call 10 items per page and in at 1 call you receive not more than 10 records it means you have all data no need more paging – Tarun Seera Jun 23 '17 at 06:06