我使用JSON数据创建一个tableview它包含分页和每个Web服务页面包含10个数据,但我想在表视图滚动然后我想加载每次只有5个项目如何可能?请给我解决方案。
我的代码
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
NSInteger currentOffset = scrollView.contentOffset.y;
NSInteger maximumOffset = scrollView.contentSize.height- scrollView.frame.size.height;
if (maximumOffset - currentOffset <= 0)
{
pageNum = pageNum + 1;
[self getData];
}
}
-(void)getData
{
NSURL * url=[NSURL URLWithString:[NSString stringWithFormat:@"http://www.truemanindiamagazine.com/webservice/news.php?page=%d",pageNum]];
dispatch_async(kBgQueue, ^{
data = [NSData dataWithContentsOfURL:url];
[self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});
}
和FetchData方法
-(void)fetchedData:(NSData *)responsedata
{
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.frame = CGRectMake(0, 0, 320, 44);
self.newsTable.tableFooterView = spinner;
if (responsedata.length > 0)
{
[spinner startAnimating];
NSError* error;
self.json = [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error];
if ([[_json objectForKey:@"data"] isKindOfClass:[NSArray class]])
{
NSArray *arr = (NSArray *)[_json objectForKey:@"data"];
[self.imageArray addObjectsFromArray:arr];
[self.newsTable reloadData];
NSLog(@"images,%@",self.imageArray);
}
}
[self.spinner stopAnimating];
self.spinner.hidesWhenStopped=YES;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.imageArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.imageArray == nil || self.imageArray.count < 1)
{
return 0;
}
else
{
return 1;
}
[self.newsTable reloadData];
}
我知道很多次被问到但是我没有得到答案。请帮助我。
答案 0 :(得分:0)
请检查以下代码
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
NSInteger currentOffset = scrollView.contentOffset.y;
NSInteger maximumOffset = scrollView.contentSize.height- scrollView.frame.size.height;
if (maximumOffset - currentOffset <= 0)
{
if([self.mTenarray count]==0) {
pageNum = pageNum + 1;
[self getData];
} else {
[self.imageArray addObjectsFromArray:self.mTenarray];
[self.mTenarray removeAllObjects];
[self.newsTable reloadData];
}
}
}
-(void)fetchedData:(NSData *)responsedata
{
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.frame = CGRectMake(0, 0, 320, 44);
self.newsTable.tableFooterView = spinner;
if (responsedata.length > 0)
{
[spinner startAnimating];
NSError* error;
self.json = [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error];
if ([[_json objectForKey:@"data"] isKindOfClass:[NSArray class]])
{
NSArray *arr = (NSArray *)[_json objectForKey:@"data"];
[self.mTenarray addObjectsFromArray:arr];
NSArray *mfivedatarray = [self.mTenarray subarrayWithRange:NSMakeRange(0, 5)];
[self.imageArray addObjectsFromArray:mfivedatarray];
self.mTenarray // remove first five objects
[self.newsTable reloadData];
NSLog(@"images,%@",self.imageArray);
}
}
[self.spinner stopAnimating];
self.spinner.hidesWhenStopped=YES;
}
答案 1 :(得分:0)
我们在服务器上执行此操作的方式是为API提供额外的&#34; 页面大小&#34;参数,以便您在请求数据时:
http://www.truemanindiamagazine.com/webservice/news.php?page=3&pageSize=5
它只返回5个结果而不是默认值,在您的情况下为10。这意味着我们不必在iOS方面进行任何花哨的过滤。