Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 重用单元UICollectionView-某些单元不';我没有被选中_Ios_Objective C_Cocoa Touch_Uicollectionview_Uicollectionviewcell - Fatal编程技术网

Ios 重用单元UICollectionView-某些单元不';我没有被选中

Ios 重用单元UICollectionView-某些单元不';我没有被选中,ios,objective-c,cocoa-touch,uicollectionview,uicollectionviewcell,Ios,Objective C,Cocoa Touch,Uicollectionview,Uicollectionviewcell,在UICollectionView中,我有一个自定义的UICollectionViewCellClass,其中prepareforeuse被默认格式人员覆盖 我有一个NSMutableArray包含NSIndexPaths来自didSelectItemAtIndexPath: 在单元格ForItemAtIndexPath:中,我重新格式化所选单元格,使其显示为选中 -(UICollectionViewCell *)collectionView:(UICollectionView *)collec

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

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

单元格ForItemAtIndexPath:中,我重新格式化所选单元格,使其显示为选中

-(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];



}

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

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

    if ([selectedCellIndexPaths containsObject:indexPath]) {
        return YES;
    }else{
        return NO;
    }
}
或者,如果您喜欢使用更简洁的语法,可以将if-else块替换为:

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

我最近在我的应用程序中遇到了完全相同的问题。UICollectionViewCell选择在iOS 8.3之前工作正常,随后我开始看到一些奇怪的行为。未实际选定的单元格将显示为已选定,而其他看似随机的单元格则无法选定

我在UICollectionViewCell子类上实现了自定义的
setSelected
prepareForResuse
方法:

 -(void)setSelected:(BOOL)selected
{
     [super setSelected:selected];

    if (selected)
    {
        [[self selectedIndicator] setHidden:NO];
    }
    else
    {
        [[self selectedIndicator] setHidden:YES];
    }
}

-(void)prepareForReuse
{
    [[self imageView] setImage:nil];
}
prepareforeuse
方法只需在自定义单元格中重置图像视图


在我的
prepareForReuse
方法中,我没有调用
[super prepareForReuse]
(根据文档,它默认不做任何操作)。当我将调用添加到
[super prepareforeuse]
时,所有选择都按预期工作。尽管苹果声明默认实现不做任何事情,但他们也建议调用super。遵循此建议解决了我的问题。

在iOS 10中,我发现在启用预取时,以编程方式清除启用了多个选择的UICollectionView是错误的。当然,在iOS 10中,预取在默认情况下是打开的。

什么是SelectedIndexPath?你在哪里创造的?我认为您还需要一个“else”部分来说明如果[self isSelectedIndexPath:indexPath]返回false,单元格应该是什么样子。它是一个检查indexPath是否包含在NSIndeXPath的NSMutableArray中的方法。当indexPath等于数组中的一个选定值时,它总是返回false。好的,您是否将“else”部分添加到if语句中以查看这是否有帮助。由于单元重用,如果if语句为false,则需要将单元的外观设置为未选择状态。如果没有更多代码,我无法解释每16个单元的情况。我在你的帖子里没有看到任何可以解释这一点的东西。您应该发布所有数据源方法和isSelectedIndexPath:方法。我尝试添加“else部分”,但它似乎没有受到影响。当我记录shouldSelectItemAtIndexPath时:我没有得到任何答案。好像还没有调用该方法。但我正在窃听那个手机!这太疯狂了,即使有了这些变化,问题依然存在。不过,非常感谢您的耐心和坚持。对不起,我错过了删除之前的准备。现在一切都很好。再次感谢您的耐心和坚持。只需:
return[selectedcellindexpath containsObject:indexPath]