gpt4 book ai didi

ios - 重用单元格 UICollectionView - 一些单元格没有被选中

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:04:33 25 4
gpt4 key购买 nike

UICollectionView 中,我有一个自定义的 UICollectionViewCellClass,其中 prepareForReuse 被默认格式化人员覆盖。

我有一个 NSMutableArray 包含来自 didSelectItemAtIndexPath:NSIndexPaths

cellForItemAtIndexPath: 中,我重新格式化选定的单元格,使它们显示为已选定。

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

ButtonCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ButtonCell" forIndexPath:indexPath];

NSString *title = self.ingredientsBook.names[indexPath.item];

cell.label.text = title;

if ([self isSelectedIndexPath:indexPath]){

cell.backgroundColor = [UIColor whiteColor];
cell.label.textColor = [UIColor blueColor];
}
return cell;

}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

self.searchButton.enabled = YES;

ButtonCell *cell = (ButtonCell *)[collectionView cellForItemAtIndexPath:indexPath];
[selectedCellIndexPaths addObject:indexPath];
NSLog(@"%@", selectedCellIndexPaths);
cell.backgroundColor = [UIColor whiteColor];
cell.label.textColor = [UIColor blueColor];

NSString *name = self.ingredientsBook.names[indexPath.item];
[self.selectedIngredientNames addObject:name];


}

问题是,当我点击第一个单元格时,无法选择第 16 个或第 17 个单元格。或者,如果我点击前三个,则无法选择最后三个。我想 didSelectItemAtIndexPath 没有被调用。

我觉得它必须非常简单,但我现在看不到。

我尝试将 NSLogs 放入 shouldSelectItemAtIndexPath 中,以了解该方法是否被调用以及该方法是否根本未被调用。当所选单元格和有问题的单元格之间有 16 个单元格的距离时,就会发生这种情况。

下面是其他数据源方法和isSelectedIndexPath:

-(BOOL)isSelectedIndexPath:(NSIndexPath *)indexPath{

for (NSIndexPath *test in selectedCellIndexPaths){
if (test == indexPath){
return YES;
}
}

return NO;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

return [self.ingredientsBook.names count];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

return 1;
}

-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath{

NSLog(@"%@", indexPath);

return YES;
}


-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{

self.searchButton.enabled = ([[collectionView indexPathsForSelectedItems] count] > 0);

ButtonCell *cell = (ButtonCell *)[collectionView cellForItemAtIndexPath:indexPath];
cell.backgroundColor = [UIColor blackColor];
cell.label.textColor = [UIColor whiteColor];

[selectedCellIndexPaths removeObject:indexPath];
NSString *name = self.ingredientsBook.names[indexPath.item];
[self.selectedIngredientNames removeObject:name];



}

最佳答案

我发现了两个问题。 prepareForReuse 方法似乎把事情搞砸了,所以我就删除了它。不过,主要问题是您实现 isSelectedIndexPath: 的方式。一旦它在您循环遍历项目时找到第一个选定的项目,它就会返回 YES 并退出循环。您想要做的只是检查 indexPath 是否包含在 selectedCellIndexPaths 数组中:

-(BOOL)isSelectedIndexPath:(NSIndexPath *)indexPath{

if ([selectedCellIndexPaths containsObject:indexPath]) {
return YES;
}else{
return NO;
}
}

或者,如果您更喜欢使用更简洁的语法,您可以将 if-else block 替换为:

return  ([selectedCellIndexPaths containsObject:indexPath])? YES : NO;

关于ios - 重用单元格 UICollectionView - 一些单元格没有被选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15851485/

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