在我的应用中,我使用PSCollectionView来获得自定义集合视图。我已准备好创建视图的所有数据,但是当我执行代码时,它不会调用方法
- (PSCollectionViewCell *)collectionView:(PSCollectionView *)collectionView cellForRowAtIndex:(NSInteger)index`
我在stackoverflow上搜索,我发现另一个人有同样的问题(link),但我不明白他是如何解决的,所以我决定写一个问题来理解如何调用这个方法。我希望有人可以帮助我找到解决这个问题的方法。
更新:
以下是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;
}
答案 0 :(得分: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
。
答案 1 :(得分:0)
看起来你必须为你的collectionView设置dataSource到你实现cellForRowAtIndex的类,例如:
collectionView.dataSource = self;