我有一个继承自deletesections
的自定义布局,用于为每个部分实现添加backgroundview。第一次显示没有问题,但是当我使用reloadData
方法删除第一部分时,在装饰视图下面显示一些单元格,奇怪的是删除数据然后调用@interface MyCustomLayout()
@property(nonatomic,strong) NSMutableArray *decorationViews;
@end
@implementation MyCustomLayout
-(NSMutableArray*)decorationViews
{
if(!_decorationViews)
{
_decorationViews=[NSMutableArray array];
}
return _decorationViews;
}
- (void)prepareLayout {
[super prepareLayout];
[self.decorationViews removeAllObjects];
NSInteger sections=[self.collectionView numberOfSections];
for(NSInteger section=0;section<sections;section++)
{
NSInteger items=[self.collectionView numberOfItemsInSection:section];
if(items>0)
{
CGRect firstItemFrame=[self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]].frame;
CGRect lastItemFrame=[self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForRow:items-1 inSection:section]].frame;
UIEdgeInsets sectionInset=self.sectionInset;
if([self.collectionView.delegate respondsToSelector:@selector(collectionView:layout:insetForSectionAtIndex:)])
{
sectionInset = [(id<UICollectionViewDelegateFlowLayout>)self.collectionView.delegate collectionView:self.collectionView layout:self insetForSectionAtIndex:section];
}
CGRect sectionFrame=CGRectUnion(firstItemFrame, lastItemFrame);
sectionFrame.origin.x=0;
sectionFrame.origin.y-=sectionInset.top;
if(self.scrollDirection==UICollectionViewScrollDirectionHorizontal)
{
sectionFrame.size.width += sectionInset.left + sectionInset.right;
sectionFrame.size.height = self.collectionView.frame.size.height;
}else
{
sectionFrame.size.width = self.collectionView.frame.size.width;
sectionFrame.size.height += sectionInset.top + sectionInset.bottom;
}
NSString *identifer=[self.delegate collectionView:self.collectionView decorationViewIdentiferAtSection:section];
if(identifer)
{
UICollectionViewLayoutAttributes *decorationAttributes=[UICollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:identifer withIndexPath:[NSIndexPath indexPathForItem:0 inSection:section]];
decorationAttributes.frame=sectionFrame;
decorationAttributes.zIndex=-1;
[self.decorationViews addObject:decorationAttributes];
}
}else
{
continue;
}
}
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
NSArray *attributes = [super layoutAttributesForElementsInRect:rect];
NSMutableArray *allAttributes = [NSMutableArray arrayWithArray:attributes];
[allAttributes addObjectsFromArray:[self.decorationViews filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
UICollectionViewLayoutAttributes *obj=evaluatedObject;
return CGRectIntersectsRect(rect, obj.frame);
}]]];
return allAttributes;
}
@protocol MyCustomLayoutDelegate <UICollectionViewDelegateFlowLayout>
-(NSString*)collectionView:(UICollectionView *)collectionView decorationViewIdentiferAtSection:(NSInteger)section;
@end
@interface MyCustomLayout : UICollectionViewFlowLayout
@property(nonatomic,weak) id<MyCustomLayoutDelegate> delegate;
方法将正常显示。
我已将代码放入GitHub:https://github.com/TonightGod/UICollectionView-DecorationView
图片: after deleteSections method called
代码:
UserDefaults.standard.set(login, forKey: "loginResponse")