我正在构建一个自定义布局集合视图。我的收藏中的每个项目都有 2 个补充视图。当我想删除一个项目时,我的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:
有谁知道可能出了什么问题?