滚动时出现在 collectionview 中的标题 up/down
titles appearing in collectionview when scroll up/down
当应用 运行 时,标题(collectionview 中的标签)没有出现。向上或向下滚动然后出现标题。同样在 numberOfItemsInSection 中,[新闻计数] 结果为 0。
fffdgdfgdfghfghgfhgfhgghghhgjgjghjghjh
- (void)viewDidLoad
{
NSLog(@"viewDidLoad ");
[super viewDidLoad];
leftMenu.target=self.revealViewController;
leftMenu.action=@selector(revealToggle:);
self.title=@"Main";
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
[UIApplication sharedApplication].networkActivityIndicatorVisible=YES;
NSURL *url=[NSURL URLWithString:@"http://www.test.com/ios/GetMainInfoByCat.php?cat_id=76"];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
//self.collectionview.dataSource=self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
data=[[NSMutableData alloc]init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)Thedata
{
[data appendData:Thedata];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"connectionDidFinishLoading ");
[UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
dnews=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
news=[dnews objectForKey:@"news_main_info_by_cat"];
NSString *str=[news valueForKey:@"img_url"];
//[self.collectionview reloadData];
//NSLog(@"%@",str);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
UIAlertView *erroMsg=[[UIAlertView alloc]initWithTitle:@"Error" message:@"Qoşula bilmədi. İnternet bağlantınızı yoxlayın" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
[erroMsg show];
[UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
[collectionView.collectionViewLayout invalidateLayout];
return 1;
}
-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
NSLog(@"%i",[news count]);
[collectionView.collectionViewLayout invalidateLayout];
return 10;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionViewCell *cell=(UICollectionView *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.newsTitle.text=[[news objectAtIndex:indexPath.row] objectForKey:@"post_title"];
//cell.newsImage.image=[NSData dataWithContentsOfURL:[NSURL URLWithString:[[news objectAtIndex:indexPath.row] objectForKey:@"img_url"]]];
//[[news objectAtIndex:indexPath.row] objectForKey:@"img_url"];
return cell;
}
--> 当您滚动 collectionview 或 tableview 时,它们会重新加载数据,因此当您获得数组后,请重新加载滚动视图,这一定会对您有所帮助。
调用重新加载数据
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"connectionDidFinishLoading ");
[UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
dnews=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
news=[dnews objectForKey:@"news_main_info_by_cat"];
NSString *str=[news valueForKey:@"img_url"];
[self.collectionview reloadData];
}
更新:确保IBoutlet连接到collectionView
当应用 运行 时,标题(collectionview 中的标签)没有出现。向上或向下滚动然后出现标题。同样在 numberOfItemsInSection 中,[新闻计数] 结果为 0。
fffdgdfgdfghfghgfhgfhgghghhgjgjghjghjh
- (void)viewDidLoad
{
NSLog(@"viewDidLoad ");
[super viewDidLoad];
leftMenu.target=self.revealViewController;
leftMenu.action=@selector(revealToggle:);
self.title=@"Main";
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
[UIApplication sharedApplication].networkActivityIndicatorVisible=YES;
NSURL *url=[NSURL URLWithString:@"http://www.test.com/ios/GetMainInfoByCat.php?cat_id=76"];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
//self.collectionview.dataSource=self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
data=[[NSMutableData alloc]init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)Thedata
{
[data appendData:Thedata];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"connectionDidFinishLoading ");
[UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
dnews=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
news=[dnews objectForKey:@"news_main_info_by_cat"];
NSString *str=[news valueForKey:@"img_url"];
//[self.collectionview reloadData];
//NSLog(@"%@",str);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
UIAlertView *erroMsg=[[UIAlertView alloc]initWithTitle:@"Error" message:@"Qoşula bilmədi. İnternet bağlantınızı yoxlayın" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
[erroMsg show];
[UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
[collectionView.collectionViewLayout invalidateLayout];
return 1;
}
-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
NSLog(@"%i",[news count]);
[collectionView.collectionViewLayout invalidateLayout];
return 10;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionViewCell *cell=(UICollectionView *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.newsTitle.text=[[news objectAtIndex:indexPath.row] objectForKey:@"post_title"];
//cell.newsImage.image=[NSData dataWithContentsOfURL:[NSURL URLWithString:[[news objectAtIndex:indexPath.row] objectForKey:@"img_url"]]];
//[[news objectAtIndex:indexPath.row] objectForKey:@"img_url"];
return cell;
}
--> 当您滚动 collectionview 或 tableview 时,它们会重新加载数据,因此当您获得数组后,请重新加载滚动视图,这一定会对您有所帮助。
调用重新加载数据
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"connectionDidFinishLoading ");
[UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
dnews=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
news=[dnews objectForKey:@"news_main_info_by_cat"];
NSString *str=[news valueForKey:@"img_url"];
[self.collectionview reloadData];
}
更新:确保IBoutlet连接到collectionView