iphone - 当用户滚动到tableView的底部时,如何向UITableView添加10条记录

标签 iphone ios objective-c uitableview nsmutablearray

当用户滚动到tableView的底部时,我尝试将10条记录添加到UITableView中。我正在使用scrollViewDidEndDecelerating:然后使用-(void)getFeed:来获取数据并将其存储在名为NSMutableArraymoreitems中。然后应将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]);

}

感谢您对此的任何帮助。

最佳答案

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

例如:

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

我希望这有助于解决问题

关于iphone - 当用户滚动到tableView的底部时,如何向UITableView添加10条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18655707/

相关文章:

objective-c - NSPanel 没有收到 mousedragged 事件

ios - “获取号码来源”按钮不允许输入超过两位数的号码

iphone - 如何获取提取时区( (GMT+05 :30) from systemTimeZone(NSTimeZone)

响应程序链中的 iPhone 自定义类/对象

ios - iOS11中如何修改NSURLRequest http方法?

ios - Phonegap应用程序可以在phonegap应用程序中使用,但不能在phonegap构建中使用

objective-c - 如何播放警报声音并呈现自定义 View 而不仅仅是通知?

iphone - 如何从上次启动中获取 NSTimeInterval 值

iphone - NSNumber 的副本没有分配新内存

ios - 保存和检索分数字符串 Cocos2d 3.0