Ios 在uitableview中调用scrollviewdidscroll方法?
我正在构建一个阅读文章的应用程序。我正在使用AFNetworking第三方库将JSON数据获取到UITableView中 假设Json链接是www.example.com&page=1给出1-10篇文章,www.example.com&page=2给出11-20篇文章,以此类推 我已经实现了分页和scrollViewDidScroll方法,这意味着当用户滚动时,它会给出下一篇十篇文章 当应用程序启动和UITableView加载scrollViewDidScroll方法调用三次但预期调用一次时,我面临一个问题 我在scrollViewDidScroll方法中使用增量变量进行分页,我说它调用三次,x值变为3,给出30篇文章 当用户再次滚动时,它会给出接下来的30篇文章。我不明白为什么在应用启动时scrollViewDidScroll方法调用了三次 这是我的代码:Ios 在uitableview中调用scrollviewdidscroll方法?,ios,objective-c,json,uitableview,afnetworking,Ios,Objective C,Json,Uitableview,Afnetworking,我正在构建一个阅读文章的应用程序。我正在使用AFNetworking第三方库将JSON数据获取到UITableView中 假设Json链接是www.example.com&page=1给出1-10篇文章,www.example.com&page=2给出11-20篇文章,以此类推 我已经实现了分页和scrollViewDidScroll方法,这意味着当用户滚动时,它会给出下一篇十篇文章 当应用程序启动和UITableView加载scrollViewDidScroll方法调用三次但预期调用一次时,我
- (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];
}
}
-(void)viewDidLoad
{
[超级视图下载];
tempJson=[[NSMutableArray alloc]init];
[自助忍者];
}
-(无效)忍者{
NSString*jsonLink=[NSString stringWithFormat:@“www.example.com&page=%d”,x];
NSURL*url=[[NSURL alloc]initWithString:jsonLink];
NSURLRequest*request=[nsurlRequestRequestWithURL:url];
AFHTTPRequestOperation*操作=[[AFHTTPRequestOperation alloc]initWithRequest:request];
operation.responseSerializer=[AFJSONResponseSerializer序列化程序];
[操作setCompletionBlockWithSuccess:^(AFHTTPRequestOperation*操作,id响应对象){
NSArray*jsonArray=(NSArray*)响应对象;
用于(NSDictionary*jsonArray中的dic){
Json*Json=[[Json ALOC]initWithDictionary:dic];
[tempJson addObject:json];
}
self.jsons=[[NSArray alloc]initWithArray:tempJson];
[self.tableView重载数据];
}失败:^(AFHTTPRequestOperation*操作,NSError*错误){
UIAlertView*alertView=[[UIAlertView alloc]initWithTitle:@“错误”
消息:[错误本地化描述]
代表:无
取消按钮:@“确定”
其他按钮:无];
[警报视图显示];
}];
[操作启动];
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
返回self.jsons.count;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态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]];
返回cell1;}
-(无效)scrollViewDidScroll:(UIScrollView*)滚动{
CGFloat currentOffset=scroll.contentOffset.y;
CGFloat maximumOffset=scroll.contentSize.height-scroll.frame.size.height;
self.tableView.contentInset=UIEdgeInsetsMake(65,0,0,0);
如果(最大偏移量-当前偏移量-(无效)scrollViewDidScroll:(UIScrollView*)滚动
滚动时被称为一杯水
你最好使用:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
或
-(无效)scrollViewDidScroll:(UIScrollView*)滚动
滚动时被称为一杯水
你最好使用:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
或
感谢它的工作性能。-(无效)ScrollViewDiEndDraging:(UIScrollView*)scrollView将减速:(BOOL)减速感谢它的工作性能。-(无效)ScrollViewDiEndDraging:(UIScrollView*)scrollView将减速:(BOOL)减速