我试图对这段代码进行分页。但是当我向下滚动它的50-60页时。 我做错了什么。
NSInteger pagesNumber = 1;
- (void)api {
AFSecurityPolicy *securityPolicy = [[AFSecurityPolicy alloc] init];
[securityPolicy setAllowInvalidCertificates:YES];
NSString *urlPath = [NSString stringWithFormat:@"http://test.com/api/index.php"];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"process":@"search_customer",@"page": [NSString stringWithFormat:@"%d",pagesNumber]};
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
[manager setSecurityPolicy:securityPolicy];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:urlPath parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSError *error = nil;
NSData *jsonData = [string dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
self.customerCards = [NSMutableArray array];
NSArray *customersArray = [dataDictionary objectForKey:@"musteri_list"];
for (NSDictionary *customersDictionary in customersArray) {
ApiClass *customer = [ApiClass customersWithTitle:[customersDictionary objectForKey:@"adi"]];
customer.tel = [customersDictionary objectForKey:@"tel"];
NSLog(@"%@",customer.tel);
[self.customerCards addObject:customer];
}
[self.tableView reloadData];
NSLog(@"GET: %@", string);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
这是我的scrollViewDidScroll方法。
- (void)scrollViewDidScroll:(UIScrollView *)scrollView_ {
if(self.tableView.contentOffset.y >= (self.tableView.contentSize.height - self.tableView.bounds.size.height))
{
pagesNumber = pagesNumber + 1;
[self api];
}
}
所以我应该在本节做什么。当我向上滚动时数据丢失。它显示的是currentPageData。
由于
答案 0 :(得分:0)
Declaration:
NSInteger pagesNumber = 1;
Call "api" method from viewDidLoad, to load data:
- (void)api {
AFSecurityPolicy *securityPolicy = [[AFSecurityPolicy alloc] init];
[securityPolicy setAllowInvalidCertificates:YES];
NSString *urlPath = [NSString stringWithFormat:@"http://test.com/api/index.php"];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"process":@"search_customer",@"page": [NSString stringWithFormat:@"%d",pagesNumber]};
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
[manager setSecurityPolicy:securityPolicy];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:urlPath parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSError *error = nil;
NSData *jsonData = [string dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
self.customerCards = [NSMutableArray array];
NSArray *customersArray = [dataDictionary objectForKey:@"musteri_list"];
for (NSDictionary *customersDictionary in customersArray) {
ApiClass *customer = [ApiClass customersWithTitle:[customersDictionary objectForKey:@"adi"]];
customer.tel = [customersDictionary objectForKey:@"tel"];
NSLog(@"%@",customer.tel);
[self.customerCards addObject:customer];
}
[self.tableView reloadData];
NSLog(@"GET: %@", string);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
//instead of using scrollViewDidScroll better use willDisplayCell:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if(self.customerCards.count>1){
if(indexPath.row == self.customerCards.count-1){
pagesNumber=pagesNumber+1;
[self api];
}
}
}