1.iOS UICollectionView cell 0间距问题的解决与分割线的设置
通过等分屏幕宽度的方式设置cell 的宽度就会出现下面的情况
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake([UIScreen mainScreen].bounds.size.width/7, 30);
}
解决办法:
新建一个类继承UICollectionViewFlowLayout,然后重写layoutAttributesForElementsInRect方法即可,代码如下:
- (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect {
NSArray *answer = [super layoutAttributesForElementsInRect:rect];
for(int i = 1; i < [answer count]; ++i) {
UICollectionViewLayoutAttributes *currentLayoutAttributes = answer[i];
UICollectionViewLayoutAttributes *prevLayoutAttributes = answer[i - 1];
NSInteger maximumSpacing = 0;
NSInteger origin = CGRectGetMaxX(prevLayoutAttributes.frame);
if(origin + maximumSpacing + currentLayoutAttributes.frame.size.width < self.collectionViewContentSize.width) {
CGRect frame = currentLayoutAttributes.frame;
frame.origin.x = origin + maximumSpacing;
currentLayoutAttributes.frame = frame;
}
}
return answer;
}
// 解决手势和collection 点击事件冲突问题
>通过判断点击的视图类型,接受类型为UIView 的时候应该是collection 的点击事件,否则按照点击手势处理,具体情况需要调试确定
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
// 判断如果点击的View是UICollectionView就可以执行手势方法,否则不执行
if ([touch.view isKindOfClass:[UICollectionView class]]) {
return YES;
}
return NO;
}