gpt4 book ai didi

ios - cellForItemAtIndexPath , sizeForItemAtIndexPath , insetForSectionAtIndex 没有被调用 iOS

转载 作者:可可西里 更新时间:2023-11-01 04:38:59 26 4
gpt4 key购买 nike

我正在使用 collectionView 来显示从 Coredatabase 获取的图像。但是,除 numberOfItemsInSectionnumberOfSectionsInCollectionView 外,没有其他方法被调用。委托(delegate)和数据源设置正确。

此外,单元格的标识符是 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];
}
}

如有任何建议,我们将不胜感激。谢谢。

最佳答案

作为@highlycaffeinated,我遇到了这个问题。但是调用 invalidateLayout 对我不起作用。

但是在数据源工作之后分配委托(delegate)。

我改变了:

self.collectionView.delegate = self;
self.collectionView.dataSource = self;

与:

self.collectionView.dataSource = self;
self.collectionView.delegate = self;

关于ios - cellForItemAtIndexPath , sizeForItemAtIndexPath , insetForSectionAtIndex 没有被调用 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23355233/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com