2

I'm trying to add 10 more records to a UITableView when the user scrolls to the bottom of the tableView. I'm using scrollViewDidEndDecelerating: and then a -(void)getFeed: to get the data and store it in a NSMutableArray called moreitems. moreitems should then be appended to self.listingNodesArray and then appended to the bottom of the table along with the records that are already there in the table.

I think my problem lies with the insertRowsAtIndexPaths: method. I'm not sure what to put after that. At the moment the app crashes on [self.tableView endUpdates];

 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
    {

        NSLog(@"scrollViewDidEndDecelerating");

        float endScrolling = scrollView.contentOffset.y + scrollView.frame.size.height;
        if (endScrolling >= scrollView.contentSize.height)
        {
            index ++;
            [self getFeed:index];
        }
    }

- (void)getFeed:(NSInteger) pageNumber
{

    NSError *requestError = nil;

    NSString *savedValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"token"];

    NSLog(@"countpage %d",index);

    NSString *stringWithToken = [NSString stringWithFormat:@"%@&%@&page=%d&token=%@",kURL,city_slug, pageNumber, savedValue];

    NSLog(@"string token is %@", stringWithToken);

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL
                                                          URLWithString:stringWithToken]];

    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&requestError];

    NSError *jsonParsingError = nil;

    if (requestError) {
        NSLog(@"sync. request failed with error: %@", requestError);
    }

    else {
        // handle data
        NSDictionary *publicData =  [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];
        moreItems = [publicData objectForKey:@"data"];

    }

    NSLog(@"moreitems data output is %@",moreItems);

    [self.listingNodesArray addObjectsFromArray:moreItems];

    int newRowIndex = [self.listingNodesArray count];

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:newRowIndex+1 inSection:0];
    NSArray *indexPaths = [NSArray arrayWithObject:indexPath];

    [self.tableView beginUpdates];

    [self.tableView  insertRowsAtIndexPaths:indexPaths
                     withRowAnimation:UITableViewRowAnimationBottom];
    [self.tableView  endUpdates];

    NSLog(@"updated self.listingNodesArray %@ and count is %d",self.listingNodesArray, [self.listingNodesArray count]);

}

thanks for any help with this.

user2588945
  • 1,681
  • 6
  • 25
  • 38
  • Duplicate Que: http://stackoverflow.com/questions/5137943/how-to-know-when-uitableview-did-scroll-to-bottom-in-iphone – Mrunal Sep 06 '13 at 10:33
  • 2
    At the both insert, you are inserting an empty array. That's your crash. And you don't need to reload data. – Desdenova Sep 06 '13 at 10:34
  • 1
    http://stackoverflow.com/questions/6023683/add-rows-to-uitableview-when-scrolled-to-bottom – Master Stroke Sep 06 '13 at 10:39
  • @Desdenova so I should add insertRowsAtIndexPaths:self.listingNodesArray to the insert? – user2588945 Sep 06 '13 at 10:41
  • No. It's asking for "where do you want to insert your new rows", so you need to supply `NSIndexPath` objects. – Desdenova Sep 06 '13 at 10:44
  • @Desdenova ok, thanks. I'm misunderstanding something though: the insertRowsAtIndexPaths: is asking for an NSArray, so how do I supply an NSIndexPath to that? thanks again for the help. been stuck on this for a while – user2588945 Sep 06 '13 at 10:51
  • A `NSArray` containing one or more `NSIndexPath` objects. – Desdenova Sep 06 '13 at 10:55
  • 1
    Take a look at the library [SVPullToRefresh](https://github.com/samvermette/SVPullToRefresh). `It has a method called [tableView addInfiniteScrollingWithActionHandler:^{ // append data to data source, insert new cells at the end of table view // call [tableView.infiniteScrollingView stopAnimating] when done }]; ` – BooRanger Sep 06 '13 at 11:06
  • @Desdenova I've edited my code above. I added the NSIndexPath and then NSArray *indexPaths = [NSArray arrayWithObject:indexPath]; but now I get *** Assertion failure in -[UITableView _endCellAnimationsWithContext:] – user2588945 Sep 06 '13 at 11:07
  • Is it too hard to post all the error message, not just it's beginning? – Morion Sep 06 '13 at 11:10
  • When you insert, your data count should match the row count of the `UITableView`. Please read Apple's `Table View Programming Guide`. – Desdenova Sep 06 '13 at 11:18

1 Answers1

0

To add more content to the array you can check in cellForRow if the indexPath.row is equals to the moreitems.count.

For example:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
   {
      CustomCellEventsWithCategoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

     if (indexPath.row == [events count]) {

         // here you can call the method that appends data to the mutable array
     }

      return cell;
}

I hope this helps solve the problem

Klinkert0728
  • 326
  • 1
  • 4
  • 14