2

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

Pankil
  • 635
  • 6
  • 20

2 Answers2

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];
        }
    }
}
Pankil
  • 635
  • 6
  • 20
KKRocks
  • 8,222
  • 1
  • 18
  • 84
  • what is totalpagecount? – Pankil Jun 23 '17 at 05:54
  • 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
  • thanks, your second answer with bool management worked for me. – Pankil Jun 23 '17 at 06:53
  • still i am facing problem in scrolling tablview to bottom – Pankil Jun 23 '17 at 13:40
  • 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
  • so i already suggested you that you need to count from server. – KKRocks Jun 26 '17 at 04:49
  • 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
  • yes but you ask to your back end developer for this count. – KKRocks Jun 26 '17 at 04:56
  • 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