カスタムレイアウトコレクションビューを構築しています。私は自分のコレクション内にアイテムごとに2つの追加ビューを持っています。また、アイテムを削除したいときは、私のlayoutAttributesForSupplementaryViewOfKind:atIndexPath:
実装が、もはや存在しない補足ビューで呼び出されます。これは、データソースに正しくマップされないインデックスパスを取得していることを意味し、範囲外の問題が発生します。追加ビューが削除されない
私はいくつかのロギングを行いました。最初の例では、いくつかのデータで開始するとき、またはコレクションビューにアイテムを挿入するときに、コレクションビューが計算される正しい論理的な方法があります。
ログの2番目のセットは、データソースからオブジェクトを削除した後のI deleteItemsAtIndexPaths:
です。
マイlayoutAttributesForElementsInRect:
次のようになります。あなたがlayoutAttributesForSupplementaryViewOfKind:atIndexPath:
を見ることができるように
MyApp[18633:70b] layoutAttributesForElementsInRect: indexPath.row:0
MyApp[18633:70b] layoutAttributesForSupplementaryViewOfKind:atIndexPath: indexPath.row:0
MyApp[18633:70b] layoutAttributesForSupplementaryViewOfKind:atIndexPath: indexPath.row:0
MyApp[18633:70b] layoutAttributesForElementsInRect: indexPath.row:1
MyApp[18633:70b] layoutAttributesForSupplementaryViewOfKind:atIndexPath: indexPath.row:1
MyApp[18633:70b] layoutAttributesForSupplementaryViewOfKind:atIndexPath: indexPath.row:1
MyApp[18633:70b] layoutAttributesForSupplementaryViewOfKind:atIndexPath: indexPath.row:2
さ:
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSMutableArray* attributes = [NSMutableArray array];
for (NSInteger i = 0 ; i < [self.myData count]; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
NSLog(@"%@ indexPath.row:%d", NSStringFromSelector(_cmd), indexPath.row);
UICollectionViewLayoutAttributes *att = [self layoutAttributesForItemAtIndexPath:indexPath];
[attributes addObject:att];
UICollectionViewLayoutAttributes *att_supp = [self layoutAttributesForSupplementaryViewOfKind:@"one_kind" atIndexPath:indexPath];
[attributes addObject:att_supp];
UICollectionViewLayoutAttributes *linesAttr = [self layoutAttributesForSupplementaryViewOfKind:@"another_kind" atIndexPath:indexPath];
[attributes addObject:linesAttr];
}
return attributes;
}
初期ログ(正しいと論理):項目を削除した後
MyApp[18546:70b] layoutAttributesForElementsInRect: indexPath.row:0
MyApp[18546:70b] layoutAttributesForSupplementaryViewOfKind:atIndexPath: indexPath.row:0
MyApp[18546:70b] layoutAttributesForSupplementaryViewOfKind:atIndexPath: indexPath.row:0
MyApp[18546:70b] layoutAttributesForElementsInRect: indexPath.row:1
MyApp[18546:70b] layoutAttributesForSupplementaryViewOfKind:atIndexPath: indexPath.row:1
MyApp[18546:70b] layoutAttributesForSupplementaryViewOfKind:atIndexPath: indexPath.row:1
MyApp[18546:70b] layoutAttributesForElementsInRect: indexPath.row:2
MyApp[18546:70b] layoutAttributesForSupplementaryViewOfKind:atIndexPath: indexPath.row:2
MyApp[18546:70b] layoutAttributesForSupplementaryViewOfKind:atIndexPath: indexPath.row:2
呼び出されるがバイパスすることによってlayoutAttributesForElementsInRect:
誰かが間違っている可能性があることを知っていますか?
いいですね、試してみましょう、ありがとう。 – Daniel
この回答がうまくいく、時にはまだまだクラッシュがある – kokemomuke
コレクションのアイテムを削除すると、レイアウトは常に古い補足ビューを要求していました。 'indexPathsToDeleteForSupplementaryViewOfKind'は私が欠けていたメソッドでした。どうもありがとう! –