gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:47:39 25 4
gpt4 key购买 nike

当用户滚动到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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com