UItableView load data on scroll

后端 未结 6 739
予麋鹿
予麋鹿 2020-12-24 03:42

In my app I am getting the data from the web-service and I have to display it in UITableView. But the condition here is I have to display only 10 records initially,then once

6条回答
  •  清歌不尽
    2020-12-24 04:09

    Just insert the new data into your datasource see below

    If you're using xml - check out XMLReader - turn XML into an NSDictionary this sample code below uses AFNetworking (which is non blocking) https://github.com/AFNetworking/AFNetworking/

    - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
    {
        if (!decelerate)
        {
            [self fetchMoreData];
        }
    }
    
    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
    {
        [self fetchMoreData];
    }
    
    
    - (void)fetchMoreData
    {
    
        if ([resultArray count] > 0)
        {
           NSArray *visiblePaths = [myTableView indexPathsForVisibleRows];
           NSIndexPath *lastRow = [visiblePaths lastObject];
    
            // Check whether or not the very last row is visible.
            NSInteger numberOfSections = [myTableView numberOfSections];
            NSInteger lastRowSection = [lastRow section];
            NSInteger lastRowRow = [lastRow row];
            NSInteger numberOfRowsInSection = [myTableView numberOfRowsInSection:lastRowSection];
    
            if (lastRowSection == numberOfSections - 1 &&  
                lastRowRow== numberOfRowsInSection - 1) {
    
                DLog(@"it's the last row");
                if ([resultArray count]%10 == 0) { // use a divider based on your pagination
                   [self fetchNextPage];
                }
    
            }
        }
    }
    
    
    -(void)getFeeds{
        ENTER_METHOD;
    
        [resultArray removeAllObjects];
        //reset this
       NSString *url = [NSString stringWithFormat:@"/webserviceurl.xml?offset=0"];
        [httpClient getPath:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    
            [self parseFeedsXMLString:operation.responseString];
            //  offset = offset + 10; // ONLY if there's results increment
    
        } failure:^(AFHTTPRequestOperation *operation, id responseObject){
            NSString *detailError=nil;
         }];
    
    }
    
    -(void)fetchNextPage{
    
        NSString *url = [NSString stringWithFormat:@"/webserviceurl.xml?offset=%d",offset];
        [httpClient getPath:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    
    
            DLog(@"operation.responseString:%@",operation.responseString);
            [self parseNextFeedsXMLString:operation.responseString];
           // offset = offset + 10; // ONLY increment if there's results 
    
    
        } failure:^(AFHTTPRequestOperation *operation, id responseObject){
    
        }];
    
    }
    
    
    
    - (void)parseFeedsXMLString:(NSString *)xmlString
    {
    
        NSError *parseError = nil;
        NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:xmlString error:&parseError];
        DLog(@"xmlDictionary:%@",xmlDictionary);
        resultArray = [[NSMutableArray arrayWithArray:[[xmlDictionary objectForKey:@"feed"] objectForKey:@"entry"]]retain];
    
        [myTableView reloadData];
    }
    
    -(void)parseNextFeedsXMLString:(NSString *)xmlString
    {
    
        NSError *parseError = nil;
        NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:xmlString error:&parseError];
        DLog(@"xmlDictionary:%@",xmlDictionary);
        //[resultArray insertObject:e atIndex:[resultArray count]];
    
        NSMutableArray *results  = [NSMutableArray arrayWithArray:[[xmlDictionary objectForKey:@"feed"] objectForKey:@"entry"]];
    
        if ([results count]) {
            page++;
            for (NSDictionary  *dict in results) {
                [resultArray insertObject:dict atIndex:[results count]];
            }
    
        }
        [myTableView reloadData];
    }
    

提交回复
热议问题