2013-11-14 14 views
10

カスタムレイアウトコレクションビューを構築しています。私は自分のコレクション内にアイテムごとに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:

誰かが間違っている可能性があることを知っていますか?

答えて

12

私も同じ問題がありました。私のレイアウトでは、コレクションビューのすべてのアイテムに対して1つの補足ビューが使用されます。私はいくつかのステップでそれを解決しました。

まず、- prepareForCollectionViewUpdates:に、更新を列挙して消滅しているアイテムを特定します。私はインデックスパスのセットをキャッシュします。

レイアウトは- indexPathsToDeleteForSupplementaryViewOfKind:を呼び出します。私はちょうど上からキャッシュされた結果を返しました。

最後に、- finalizeCollectionViewUpdatesに、私はキャッシュをクリアします。私はこのステップがオプションであると考えます。

私は- indexPathsToInsertForSupplementaryViewOfKind:のために一貫性のためにこれも行いましたが、これはうまく機能しませんでした。

+0

いいですね、試してみましょう、ありがとう。 – Daniel

+0

この回答がうまくいく、時にはまだまだクラッシュがある – kokemomuke

+0

コレクションのアイテムを削除すると、レイアウトは常に古い補足ビューを要求していました。 'indexPathsToDeleteForSupplementaryViewOfKind'は私が欠けていたメソッドでした。どうもありがとう! –

関連する問題