【发布时间】:2013-09-10 10:21:54
【问题描述】:
当用户滚动到 tableView 的底部时,我正在尝试向 UITableView 添加 10 条记录。我使用scrollViewDidEndDecelerating: 和-(void)getFeed: 来获取数据并将其存储在名为moreitems 的NSMutableArray 中。然后应将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]);
}
感谢您对此的任何帮助。
【问题讨论】:
-
在两次插入中,您插入的是一个空数组。那是你的崩溃。而且您不需要重新加载数据。
-
@Desdenova 所以我应该将 insertRowsAtIndexPaths:self.listingNodesArray 添加到插入中?
-
看看库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 }];
标签: iphone ios objective-c uitableview nsmutablearray