在URL连接完成之前调用的CollectionView委托

时间:2015-09-18 22:31:44

标签: ios objective-c uicollectionview nsurl

我正在使用URL Connection实现CollectionView。

我遇到的问题如下:在URL连接完成之前调用collectionView委托。因此,Collection View不会显示任何数据或如何重新加载集合视图。

这是我的实施。

-(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的班级中被定义为__weak IBOutlet UICollectionView *_collectionView;我怎样才能让它发挥作用?

1 个答案:

答案 0 :(得分:2)

在你的connectionDidFinishLoading结束时:调用[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];
    }];
}