gpt4 book ai didi

ios - 在 collectionview 上滚动后 dequeueReusableCellWithReuseIdentifier 出错了

转载 作者:行者123 更新时间:2023-11-29 02:58:55 25 4
gpt4 key购买 nike

enter image description here

我在 UICollectionViewCell 中的 UICollectionView 遇到了这个问题:最后一个 UICollectionViewCell 没有填充 13 - 18,而是复制了第一个。我知道它与“dequeueReusableCellWithReuseIdentifier”有关,但如果我不检查下面的标签,我的整个收藏就会一团糟。

我一直在努力解决这个问题并在谷歌上搜索了大约一天,但我找不到解决方法。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellID=@"pageCell";
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
int no = indexPath.item;
if(collectionView == _pageCollectionView)
{
//Collection pages
cell.tag=no+1;
UICollectionViewFlowLayout *layoutMods=[[UICollectionViewFlowLayout alloc] init];
[layoutMods setSectionInset:UIEdgeInsetsMake(0, 0, 0, 0)];
[layoutMods setMinimumInteritemSpacing:0];
[layoutMods setMinimumLineSpacing:0];
[layoutMods setItemSize:CGSizeMake(125, 125)];
_modsCollectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0,0, _pageCollectionView.frame.size.width, _pageCollectionView.frame.size.height) collectionViewLayout:layoutMods];
[_modsCollectionView setDataSource:self];
[_modsCollectionView setDelegate:self];
_modsCollectionView.pagingEnabled = YES;
[_modsCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"pageCell"];

NSMutableArray *randColor = [[NSMutableArray alloc] init];
for(int i=0;i<3;i++)
{
int lowerBound = 0;
int upperBound = 255;
NSNumber *rndValue = [NSNumber numberWithFloat:(lowerBound + arc4random() % (upperBound - lowerBound) )/255.0];
[randColor insertObject:rndValue atIndex:i];
}

float r = [[randColor objectAtIndex:0] floatValue];
float g = [[randColor objectAtIndex:1] floatValue];
float b = [[randColor objectAtIndex:2] floatValue];
_modsCollectionView.backgroundColor = [UIColor colorWithRed:r green:g blue:b alpha:1];
[cell addSubview:_modsCollectionView];
}
else
{
if(cell.tag == 0)
{
cell.tag = 2;

NSMutableDictionary *mod=[modules objectAtIndex:modulePointer];

cell.backgroundColor=[mod objectForKey:@"color"];

NSString *slug = [mod objectForKey:@"slug"];

NSString *imgName=[NSString stringWithFormat:slug,@".png"];
UIImageView *customBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imgName]];
customBackground.frame = CGRectMake(0,0, 125, 125);
[cell addSubview:customBackground];
customBackground.contentMode = UIViewContentModeScaleAspectFit;
customBackground.clipsToBounds = YES;
modulePointer++;
}
}
return cell;
}

我做错了什么?

最佳答案

明白了!我必须在 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 中使用 View 标签来施展魔法。

if(collectionView == _pageCollectionView) 中,我输入:

for(id subview in [cell subviews])
{
[subview removeFromSuperview];
}
cell.tag=itemsPerScreen*no;

然后在下面的else中:

int thisIndex=[collectionView superview].tag+no;
NSMutableDictionary *mod=[modules objectAtIndex:thisIndex];

关于ios - 在 collectionview 上滚动后 dequeueReusableCellWithReuseIdentifier 出错了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23521282/

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