2

UITableView当用户滚动到 tableView 的底部时,我正在尝试向 a 添加 10 多条记录。我正在使用scrollViewDidEndDecelerating: 然后 a-(void)getFeed:获取数据并将其存储在一个NSMutableArray被调用的moreitems. moreitems然后应该self.listingNodesArray与表中已经存在的记录一起附加到表的底部,然后再附加到表的底部。

我认为我的问题在于insertRowsAtIndexPaths:方法。我不知道在那之后放什么。目前应用程序崩溃[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]);

}

感谢您对此的任何帮助。

4

1 回答 1

0

如果 indexPath.row 等于 moreitems.count,您可以在 cellForRow 中检查向数组添加更多内容。

例如:

- (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;
}

我希望这有助于解决问题

于 2015-08-02T04:23:56.417 回答