12207480 / TYPagerController

page scroll view and controller,simple,high custom,and have many tabBar styles,,support Objective-C and swift
MIT License
1.37k stars 232 forks source link

请解决该Bug index = -1 Bug #135

Closed ShouBinCheng closed 5 years ago

ShouBinCheng commented 5 years ago

在真机情况向调用经常重现。index==-1 报僵尸内存错误在这一句

UICollectionViewLayoutAttributes * cellAttrs = [_collectionView layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]];

app 启动时如下去reload 重现bug

dispatch_async(dispatch_get_main_queue(), ^{
     weakSelf.DataSource = newDataSource;//这里的count值由开始的0到大于0。
    [weakSelf.pagerBar reloadData];
});

修复

- (CGRect)cellFrameWithIndex:(NSInteger)index
{
    // ***修复
    if (index < 0) {
        return CGRectZero;
    }
    // ***
    if (index >= _countOfItems) {
        return CGRectZero;
    }
    UICollectionViewLayoutAttributes * cellAttrs = [_collectionView layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]];
    if (!cellAttrs) {
        return CGRectZero;
    }
    return cellAttrs.frame;
}