JSON 데이터를 UITableView로 가져 오기 위해 AFNetworking 써드 파티 라이브러리를 사용하고 있습니다.scrollViewDidScroll 메서드로 UITableView에서 페이지 매김을 구현하고 있습니까?
Json 링크는 www.example.com입니다. & 페이지 = 1은 1-10 개의 기사를 제공하고 www.example.com은 & 페이지 = 2는 11-20 개의 기사를 제공합니다.
페이지 매김을 구현했으며 scrollViewDidScroll 메서드는 사용자가 스크롤 할 때 다음 10 개의 기사를 제공한다는 것을 의미합니다.
앱 실행 및 UITableView가 scrollViewDidScroll 메서드를로드 할 때 세 번 호출되지만 예상 호출을 한 번 호출하면 문제가 발생합니다.
scrollViewDidScroll 메서드에서 페이지 매김에 대한 증분 변수를 사용하고 있는데 3 번 호출하고 x 값이 3이고 30 개의 기사가 포함되어 있습니다.
사용자가 다시 스크롤하면 다음 30 개 기사가 제공됩니다. 앱이 시작될 때 scrollViewDidScroll 메서드가 세 번 호출 된 이유를 파악할 수 없습니다. , 이것은 50 개 세포 및 페이지 아래로 사용자가 스크롤로있는 tableView를 초기화하는 간단한 코드입니다
- (void)viewDidLoad
{
[super viewDidLoad];
tempJson = [[NSMutableArray alloc] init];
[self loadNinjas];
}
- (void)loadNinjas {
NSString *jsonLink=[NSString stringWithFormat:@"www.example.com&page=%d",x];
NSURL *url = [[NSURL alloc] initWithString:jsonLink];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSArray *jsonArray = (NSArray *)responseObject;
for (NSDictionary *dic in jsonArray) {
Json *json = [[Json alloc] initWithDictionary:dic];
[tempJson addObject:json];
}
self.jsons = [[NSArray alloc] initWithArray:tempJson];
[self.tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];
}];
[operation start];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.jsons.count ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *Cellidentifier1 = @"ysTableViewCell";
ysTableViewCell *cell1 = [tableView
dequeueReusableCellWithIdentifier:Cellidentifier1 forIndexPath:indexPath];
cell1.TitleLabel1.text = [self.jsons[indexPath.row] title];
cell1.AuthorLabel1.text = [self.jsons[indexPath.row] author];
[cell1.ThumbImage1 setImageWithURL:[NSURL URLWithString:
[self.jsons[indexPath.row] a_image]]];
return cell1;}
- (void)scrollViewDidScroll: (UIScrollView*)scroll {
CGFloat currentOffset = scroll.contentOffset.y;
CGFloat maximumOffset = scroll.contentSize.height - scroll.frame.size.height;
self.tableView.contentInset = UIEdgeInsetsMake(65, 0, 0, 0);
if (maximumOffset - currentOffset <= -60.0) {
x++;
[self loadNinjas];
[self.tableView addInfiniteScrollingWithActionHandler:^{
}];
[self.tableView reloadData];
}
}