子类化UICollectionViewFlowLayout有时会在iOS6上崩溃

时间:2014-03-26 11:07:23

标签: ios objective-c ios7 ios6 uicollectionview

我想要一个UICollectionView,其中flowlayout方向是水平的。我已经实现了targetContentOffsetForProposedContentOffset方法:withScrollingVelocity:to always" snap"在表格中滚动时,集合视图中单元格的视图。

我已经像这样继承了UICollectionViewFlowLayout (使用我在此处实现的方法的代码:targetContentOffsetForProposedContentOffset:withScrollingVelocity without subclassing UICollectionViewFlowLayout

@implementation SnappingFlowLayout

-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if(self)
    {
        [self customInit];
    }
    return self;
}

-(void)customInit
{
//    self.itemSize = CGSizeMake(75.0, 75.0);
    self.minimumInteritemSpacing = 0;
    self.minimumLineSpacing = 0;
    self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
}

- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
{
    CGFloat offsetAdjustment = MAXFLOAT;
    CGFloat horizontalOffset = proposedContentOffset.x;

    CGRect targetRect = CGRectMake(proposedContentOffset.x, 0, self.collectionView.bounds.size.width, self.collectionView.bounds.size.height);

    NSArray *array = [super layoutAttributesForElementsInRect:targetRect];

    for (UICollectionViewLayoutAttributes *layoutAttributes in array) {
        CGFloat itemOffset = layoutAttributes.frame.origin.x;
        if (ABS(itemOffset - horizontalOffset) < ABS(offsetAdjustment)) {
            offsetAdjustment = itemOffset - horizontalOffset;
        }
    }
    int x = (proposedContentOffset.x + offsetAdjustment) >= 0 ? (proposedContentOffset.x + offsetAdjustment) : 0;
    return CGPointMake(x, proposedContentOffset.y);
}

当我将此作为我的UICollectionView的自定义布局使用时有时在集合视图上调用方法时会出现以下错误,但仅适用于iOS6

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UICollectionViewUpdateItem action]: unrecognized selector sent to instance

这些方法有时会导致错误:

[self.collectionView scrollToItemAtIndexPath:newIndexPath atScrollPosition:UICollectionViewScrollPositionRight animated:YES];

[self.collectionView deleteItemsAtIndexPaths:indexPaths];

我的indexPaths没有任何问题。它们在iOS7上运行得很好,大部分时间都在iOS6中运行。

我的子类实现中是否有一些明显的错误导致了这种情况?

谢谢。

0 个答案:

没有答案