0

在我的应用程序中,我将使用 PSCollectionView,但它不会调用委托方法。我是这样做的:

  1. 我读了一个 JSON 文件
  2. 我解析这个 JSON 以获取名称和图像 url
  3. 我将设备连接到图像 url 以使用您在此问题末尾找到的代码获取宽度和高度

在此操作之后,我需要调用委托方法来获取 PSCollectionViewCell 的数量,定义每个单元格的高度并用图像和名称填充单元格。我实现了委托方法,但它不执行它们。我想我的问题是因为当它想要创建和定义单元格的特征时,包含填充单元格的信息的数组仍然是空的。我该如何解决这个问题?

代码:

- (void) loadImageFromWeb:(NSString *)urlImg forName:(NSString*)name {
    NSURL* url = [NSURL URLWithString:urlImg];
    //NSURLRequest* request = [NSURLRequest requestWithURL:url];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

    NSString *authCredentials =@"reply:reply";
    NSString *authValue = [NSString stringWithFormat:@"Basic %@",[authCredentials base64EncodedStringWithWrapWidth:0]];
    [request setValue:authValue forHTTPHeaderField:@"Authorization"];

    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse * response,
                                               NSData * data,
                                               NSError * error) {
                               if (!error){
                                   image = [[UIImage alloc] initWithData:data];
                                   imageWidth = image.size.width;
                                   imageHeight = image.size.height;
                                   imgWidth = [NSString stringWithFormat:@"%f", imageWidth];
                                   imgHeight = [NSString stringWithFormat:@"%f", imageHeight];
                                   self.dictWithDataForPSCollectionView = @{@"title": name,
                                                                            @"width": imgWidth,
                                                                            @"height": imgHeight};
                                   [self.arrayWithData addObject:self.dictWithDataForPSCollectionView];
                                   NSLog(@"DATA ARRAY: %@", self.arrayWithData);
                               } else {
                                   NSLog(@"ERRORE: %@", error);
                               }

                           }];
}

谁能帮我解决这个问题?谢谢!

4

2 回答 2

1

您的问题是,在您设置数组以解决您的问题之前调用了委托和数据源,当您像这样设置数组时,您必须重新加载数据:

-(void)setArrayWithData:(NSArray *)arrayWithData{

    if(_arrayWithData != arrayWithData){
       _arrayWithData = arrayWithData;
    }

    [_yourCollectionView reloadData];

}
于 2013-11-11T13:43:46.040 回答
0

确保通过执行 myCollectionView.delegate = self; 将 collectionview 连接到视图控制器。

或者

在界面构建器中控制从集合视图到视图控制器的拖动

于 2013-11-11T13:30:14.220 回答