0

在我的应用程序中,我使用PSCollectionView来拥有一个自定义集合视图。我已经准备好创建视图的所有数据,但是当我执行代码时它不会调用该方法

- (PSCollectionViewCell *)collectionView:(PSCollectionView *)collectionView cellForRowAtIndex:(NSInteger)index`

我在stackoverflow上搜索,发现另一个人有同样的问题(链接),但我不明白他是如何解决的,所以我决定写一个问题来了解如何调用这个方法。我希望有人可以帮助我找到解决此问题的方法。

更新:

这是viewDidLoad代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    tempArray = [[NSMutableArray alloc]init];
    self.navBar.title = self.category;
    NSString *stringUrl = [NSString stringWithFormat:@"http://54.204.6.246/magento8/api/rest/products/?category_id=%d", self.categoryId];
    [self sendRequestToURL:stringUrl withMethod:@"GET"];
    // Inizio sezione di codice per la creazione della PSCOLLECTIONVIEW
    psView = [[PSCollectionView alloc]init];
    // Imposto il delegato per la gestione della scrollView
    psView.delegate = self;
    // Imposto il delegato per la collection view
    psView.collectionViewDelegate = self;
    // Imposto il delegato per il data source della collection view
    psView.collectionViewDataSource = self;

    psView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

    // Controllo il tipo di device e a seconda del dispositivo trovato creo le colonne e le righe
    if ([[UIDevice currentDevice].model isEqualToString:@"iPhone"]) {
        psView.numColsPortrait = 2;
        psView.numColsLandscape = 4;
    } else {
        psView.numColsPortrait = 2;
        psView.numColsLandscape = 4;
    }

    // Aggiungo la vista appena creata
    [self.view addSubview:psView];
}

这是委托代码:

#pragma mark - PSCollection Delegate e Data Source

- (NSInteger)numberOfRowsInCollectionView:(PSCollectionView *)collectionView {
    return [self.arrayWithData count];
}

- (CGFloat)collectionView:(PSCollectionView *)collectionView heightForRowAtIndex:(NSInteger)index {
//    NSDictionary *item = [self.arrayWithData objectAtIndex:index];
//    
//    return [ProductViewCell heightForViewWithObject:item inColumnWidth:psView.colWidth];
    return 100.0f;
}

- (PSCollectionViewCell *)collectionView:(PSCollectionView *)collectionView cellForRowAtIndex:(NSInteger)index {
    ProductViewCell *cell = (ProductViewCell *)[psView dequeueReusableViewForClass:nil];
    if (!cell) {
        cell = [[ProductViewCell alloc]initWithFrame:CGRectZero];
    }
    cell.labelName.text = [[self.arrayWithData objectAtIndex:index]objectForKey:@"name"];
    NSURL * url = [NSURL URLWithString:[[self.arrayWithData objectAtIndex:index]objectForKey:@"url"]];

    [cell.productImage setImageWithURL:url
                             completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {

                                 //[[[SDWebImageManager sharedManager] imageCache] removeImageForKey:yacht.thumbnail fromDisk:NO];
                                 [[[SDWebImageManager sharedManager] imageCache] clearMemory];


                             }];
    return cell;
}
4

2 回答 2

2

必须有一个委托问题。看@Github 的使用示例:

self.collectionView = [[PSCollectionView alloc] initWithFrame:CGRectZero];
self.collectionView.delegate = self; // This is for UIScrollViewDelegate
self.collectionView.collectionViewDelegate = self;
self.collectionView.collectionViewDataSource = self;
self.collectionView.backgroundColor = [UIColor clearColor];
self.collectionView.autoresizingMask = ~UIViewAutoresizingNone;

所以你必须设置委托 3 次。你都分配了吗?

还有一件事。基于 Github 中的文档:

- (Class)collectionView:(PSCollectionView *)collectionView cellClassForRowAtIndex:(NSInteger)index {
    return [PSCollectionViewCell class];
}

- (NSInteger)numberOfRowsInCollectionView:(PSCollectionView *)collectionView {
    return 0;
}

- (UIView *)collectionView:(PSCollectionView *)collectionView cellForRowAtIndex:(NSInteger)index {
    return nil;
}

- (CGFloat)collectionView:(PSCollectionView *)collectionView heightForRowAtIndex:(NSInteger)index {
    return 0.0;
}

检查是否正在调用所有其他方法。检查输出,numberOfRowsInCollectionView因为如果您0在此方法中返回,您cellForRowAtIntex:将永远不会被调用。

编辑 尝试换行:

psView = [[PSCollectionView alloc]init];

至:

psView = [[PSCollectionView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)];

这应该让你全屏PSCollectionView

于 2013-11-12T09:13:39.050 回答
0

看起来你必须为你的collectionView设置dataSource到你实现cellForRowAtIndex的类,例如:

collectionView.dataSource = self;
于 2013-11-12T09:03:21.910 回答