3

UICollectionViewは、カスタムUICollectionViewCellClassの があり、prepareForReuseはデフォルトの書式設定スタッフ用にオーバーライドされます。

didSelectItemAtIndexPath :からの内容NSMutableArrayがあります。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 番目のセルを選択できないことです。または、最初の 3 つをタップすると、最後の 3 つを選択できません。は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];



}
4

3 に答える 3

2

2つの問題が見つかりました。prepareForReuse メソッドが問題を起こしているようだったので、削除しました。ただし、主な問題は、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;
于 2013-04-06T20:25:44.760 に答える
0

iOS 10 では、複数選択が有効になっている UICollectionView をプログラムでクリアすると、プリフェッチが有効になっているときにバグが発生することがわかりました。もちろん、iOS 10 ではプリフェッチがデフォルトでオンになっています。

于 2016-11-17T04:26:42.243 に答える