在我的应用程序中,我使用 PSCollectionView拥有自定义 Collection View 。我已经准备好创建 View 的所有数据,但是当我执行代码时它没有调用方法
- (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];
}
这是委托(delegate)代码:
#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;
}
最佳答案
必须有委托(delegate)问题。查看@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;
所以你必须设置delegate 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
。
关于ios - PSCollectionView 不执行 cellForRowAtIndex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19924773/