我使用collectionView
在从Coredatabase
抓取后显示图片。但是,除了numberOfItemsInSection
& numberOfSectionsInCollectionView
。代理和数据源设置正确。
单元格的标识符是photoCell,类是JLTPhotoCell
。
- (void)viewDidLoad
{
[super viewDidLoad];
_thumbNails = [[NSArray alloc]init]; // its declared ,don't worry about this
_thumbNails = [[LADataModelController getSingleton] getAllSignatures];
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
[layout setItemSize:( CGSizeMake(50,50))];
[_collectionView setCollectionViewLayout:layout];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[_collectionView registerClass:[JLTPhotoCell class] forCellWithReuseIdentifier:@"photoCell"];
[self.view addSubview:self.collectionView];
self.collectionView.allowsSelection = true;
}
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section
{
NSLog(@"count %i" , _thumbNails.count);
return _thumbNails.count;
}
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView
{
return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (_thumbNails.count > 0)
{
JLTPhotoCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"photoCell" forIndexPath:indexPath];
NSData *thumbnailData = ((LASignature*)[_thumbNails objectAtIndex:indexPath.row]).signatureImage;
UIImage* thumbnailImage = [UIImage imageWithData:thumbnailData];
[cell setThumbnail:thumbnailImage];
NSLog(@"image:%@",thumbnailImage);
if (!_buttonSelect.hidden)
{
[cell performSelected:FALSE];
}
return cell;
}
return nil;
}
#pragma mark UICollectionViewDelegate Methods
-(void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
_lastSelectedSign = [_thumbNails objectAtIndex:indexPath.row];
if (!self.buttonSelect.hidden)
{
JLTPhotoCell *cell = (JLTPhotoCell *)[_collectionView cellForItemAtIndexPath:indexPath];
cell.selected = FALSE;
}
else
{
[_selectedImages addObject:[_thumbNails objectAtIndex:indexPath.row]];
JLTPhotoCell *cell = (JLTPhotoCell *)[_collectionView cellForItemAtIndexPath:indexPath];
[cell performSelected:TRUE];
// _buttonDelete.enabled = _selectedImages.count > 0;
}
}
-(void) collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
[_selectedImages removeObject:[_thumbNails objectAtIndex:indexPath.row]];
JLTPhotoCell *cell = (JLTPhotoCell *)[_collectionView cellForItemAtIndexPath:indexPath];
[cell performSelected:FALSE];
_buttonDelete.enabled = _selectedImages.count > 0;
}
#pragma mark - UICollectionViewDelegateFlowLayout
-(CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(50,50) ;
}
-(UIEdgeInsets) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewFlowLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
collectionViewLayout.minimumInteritemSpacing=1;
collectionViewLayout.minimumLineSpacing =2;
return UIEdgeInsetsMake(1, 1, 1, 1);
}
jltPhotoCell Class
-(void) setThumbnail:(UIImage *) thumbnail
{
[self setThumbnail:thumbnail andWithSize:(CGSizeMake(50,50))];
}
-(void) setThumbnail:(UIImage *) thumbnail andWithSize:(CGSize) size
{
UIImageView *thumbImageView = [[UIImageView alloc] initWithImage:thumbnail];
thumbImageView.frame = CGRectMake(0,0,size.width,size.height);
thumbImageView.userInteractionEnabled = YES;
[self.contentView addSubview:thumbImageView];
}
-(void) performSelected:(bool)selected
{
if (selected)
{
UIImageView *deleteImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"delete"]];
deleteImageView.frame = CGRectMake(self.contentView.frame.size.width - 35, 3, 32, 32);
deleteImageView.tag = 123;
[self.contentView addSubview:deleteImageView];
}
else
{
UIView *v = [self.contentView viewWithTag:123];
v.hidden = YES;
[self.contentView bringSubviewToFront:v];
[v removeFromSuperview];
}
}
任何建议都将不胜感激。感谢。
答案 0 :(得分:4)
@highlycaffeinated我遇到了这个问题。但是,调用invalidateLayout并不适合我。
但是,在数据源工作之后分配代理。
我改变了:
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
使用:
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
答案 1 :(得分:1)
我遇到了同样的问题。在分配数据源后,我可以通过调用布局invalidateLayout
来解决它。
答案 2 :(得分:0)
仅适用于会遇到类似UICollectionViews问题的人,尤其是在以编程方式执行此操作时。所以我有同样的问题,终于发现我在数组中有零元素,应该在collectionView中填充。因此,在阅读文档之后,它说集合视图将不会显示,除非从阵列中显示至少一个元素,否则它将一直给你黑屏。在这种情况下可能发生的另一个问题是sizeForItemAtIndexPath将在cellForItemAtIndexPath之前被调用,因为像标签和图像的单元格内容不会占用自己的高度,而是单元格本身的高度。希望这个答案会帮助周围的人。