我正在構建自定義佈局集合視圖。在我的收藏中,每件商品都有兩個補充視圖。而當我想刪除一個項目時,我的layoutAttributesForSupplementaryViewOfKind:atIndexPath:
實現被稱爲補充視圖,應該不再存在。這意味着我得到一個索引路徑,該路徑沒有正確映射到我的數據源,並且出現了一個超出界限的問題。補充視圖不被刪除
我做了一些記錄和接下來的初審是正確的,合乎邏輯的方式,當我們有一些數據開始,或當我們插入項目到集合查看集合視圖進行計算。
第二組日誌是當我從我的數據源中刪除一個對象後,我deleteItemsAtIndexPaths:
。
我layoutAttributesForElementsInRect:
看起來是這樣的:
- (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
刪除項目後:
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
正如你可以看到layoutAttributesForSupplementaryViewOfKind:atIndexPath:
是所謂但繞過layoutAttributesForElementsInRect:
有誰知道什麼可能會出錯?
聽起來不錯,我會試試看,謝謝。 – Daniel
有些時候,這個答案有效,有時候不會,還有一些崩潰 – kokemomuke
當我刪除收集的項目時,佈局總是要求一箇舊的補充視圖。 'indexPathsToDeleteForSupplementaryViewOfKind'就是我缺少的方法。非常感謝! –