在我的应用程序中,我使用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;
}
最佳答案
必须有一个委托问题。看@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
。