Ios 在URL连接完成之前调用CollectionView委托
我正在使用URL连接实现CollectionView 我遇到的问题如下:在URL连接完成之前调用collectionView委托。因此,“集合”视图无法显示任何数据,也无法重新加载“集合”视图 这是我的实现Ios 在URL连接完成之前调用CollectionView委托,ios,objective-c,uicollectionview,nsurl,Ios,Objective C,Uicollectionview,Nsurl,我正在使用URL连接实现CollectionView 我遇到的问题如下:在URL连接完成之前调用collectionView委托。因此,“集合”视图无法显示任何数据,也无法重新加载“集合”视图 这是我的实现 -(id)init{ self = [super init]; if (self){ [self loadFromURL]; } return self; } -(void)loadFromURL{ NSURLRequest *th
-(id)init{
self = [super init];
if (self){
[self loadFromURL];
}
return self;
}
-(void)loadFromURL{
NSURLRequest *theRequest = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"http://www.appybyte.com/satgitsin/products.php?zip=77072"]];
NSURLConnection *theConnection=[[NSURLConnection alloc]
initWithRequest:theRequest delegate:self];
if(theConnection){
responseData = [[NSMutableData alloc] init];
} else {
NSLog(@"failed");
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[responseData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSString *msg = [NSString stringWithFormat:@"Failed: %@", [error description]];
NSLog(@"%@",msg);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&myError];
self.pElements = [[NSMutableArray alloc] init];
for (NSDictionary *aModuleDict in res){
PMosaicData *aMosaicModule = [[PMosaicData alloc] initWithDictionary:aModuleDict];
[self.pElements addObject:aMosaicModule];
}
}
#pragma mark - UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return [self.pElements count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"cell";
PMosaicCell *pCell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
PMosaicData *pData=[self.pElements objectAtIndex:indexPath.row];
pCell.pMosaicData = pData;
return pCell;
}
顺便说一句,我的
collectionView
在另一个名为CustomViewController.h
的类中定义为\uuuuuuuicollectionview*\ucollectionview代码>我如何才能让它工作 在连接结束时,调用[self.collectionView reloadData]
我建议使用NSURLConnection的类方法sendAsynchronousRequest:queue:options:error:而不是委托模式,因为您没有执行任何自定义缓冲逻辑
最终的代码应该如下所示:
-(void)loadFromURL {
NSURL *url = [NSURL URLWithString:@"http://www.appybyte.com/satgitsin/products.php?zip=77072"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
__weak typeof(self) weakSelf = self;
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&myError];
// probably should handle the error here
if (myError) {
}
weakSelf.pElements = [[NSMutableArray alloc] init];
for (NSDictionary *aModuleDict in res){
PMosaicData *aMosaicModule = [[PMosaicData alloc] initWithDictionary:aModuleDict];
[weakSelf.pElements addObject:aMosaicModule];
}
[weakSelf.collectionView reloadData];
}];
}
我的collectionView
在另一个名为CustomViewController.h
的类中定义为\uuuuuuuuuuuCollectionView*\uCollectionView代码>我如何才能让它工作?顺便说一句,我对你的努力投了更高的票。没有看到你的全部实现,这是一个很难回答的问题。最简单的方法是在此类中引用CustomViewController实例,然后只需执行[self.CustomViewController.collectionView reloadData]。如果这不可行,您可以轻松实现通知、委托模式、回调块或KVOShall的使用,我将代码共享给您运行?你能分享你的电子邮件吗?这样其他人也可以跟随/帮助,我更喜欢你将代码粘贴到github gist中。如果它更有意义,因为它是多个文件,你也可以为它创建一个完整的回购协议。