gpt4 book ai didi

ios - 具有动态单元格内容的 UICollectionView - 让单元格记住状态

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

我有一个包含动态内容的 UICollectionView。我遇到的问题是,当单元格 dequeued 时,它忘记了它的状态。在我的 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 我有一个 bool 值,当它为真时,它会变成真实的图像,但当它是false,它变成一个假图像。但是,当我向下滚动并向上滚动时,细胞会忘记那里的状态。有没有办法让细胞记住它的状态?这是我里面的东西

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath   

SalesCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
SaleImage * saleImage = [self.SaleObjs objectAtIndex:indexPath.row];
cell.bgImageView.image = [UIImage imageNamed:saleImage.imageName];
if (saleImage.isBookable) {
cell.isBookable = YES;
}
else{
cell.isBookable=NO;
}

return cell;
}

我对此有补救措施,但它会影响性能。我将其添加到我的自定义单元格中;

-(void)prepareForReuse{
[super prepareForReuse];


[self setNeedsLayout];
}

这是我的 Collection View 单元格;

@implementation SalesCollectionViewCell


- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {

// Initialization code
self.layer.cornerRadius = 6.0;
self.bgImageView = [[UIImageView alloc] init];
self.book = [[UILabel alloc] init];
[self.contentView insertSubview:self.bgImageView atIndex:0];


return self;
}

-(void)layoutSubviews{

[super layoutSubviews];

if (self.isBookable) {
self.book.frame =CGRectMake(0, self.bounds.size.height - 41, 140, 41);
self.book.text = @"Book this Item";
self.book.textColor = [UIColor whiteColor];
self.book.adjustsFontSizeToFitWidth=YES;
self.book.textAlignment = NSTextAlignmentCenter;
self.book.backgroundColor= [UIColor darkGrayColor];
self.book.font = [UIFont fontWithName:kAppFont size:17.0];
[self.contentView insertSubview:self.book atIndex:2];

self.bgImageView.frame =CGRectMake(0, 0, 140, self.bounds.size.height - self.book.bounds.size.height);


}
else{
self.bgImageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);

}



}

-(void)prepareForReuse{
[super prepareForReuse];

[self.book removeFromSuperview];
[self setNeedsLayout];


}

最佳答案

细胞不应该“记住”它们的状态; Collection View 或 TableView 数据源应该。在相应的 cellForIndexPath 方法中,您应该设置单元格的当前状态,并让它根据需要自行配置。

关于ios - 具有动态单元格内容的 UICollectionView - 让单元格记住状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24570702/

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