UICollectionViewLayout(从 Swift 转换为 Objective-C)

UICollectionViewLayout (Converting from Swift to Objective-C)

我目前正在学习创建自定义 UICollectionViewLayout 的教程 (https://www.raywenderlich.com/99087/swift-expanding-cells-ios-collection-views),问题是教程是用 Swift 编写的,但我正在尝试将其转换为 Objective-C 用于我的项目。

将教程复制到 Objective-C 的第一部分很好,我已经到了这个阶段。

但是,在第二部分中,当我们假设创建自定义 UICollectionViewLayout 时,在故事板中将 CollectionView 的布局更改为自定义并设置自定义 class,出现空白屏幕。

以下是我从 Swift 到 Objective-C 的教程中复制的代码:

@implementation TimetableCustomLayout{
    NSMutableArray *cache;
}

-(NSInteger)featuredItemIndex{

CGFloat dragOffset = 180;
    return MAX(0, self.collectionView.contentOffset.y - dragOffset);
}

-(CGFloat)nextItemPercentageOffset{
    CGFloat dragOffset = 180;
    return (self.collectionView.contentOffset.y / dragOffset) - [self    featuredItemIndex];
}

-(CGFloat)width{
    return CGRectGetWidth(self.collectionView.bounds);
}

-(CGFloat)height{
    return CGRectGetHeight(self.collectionView.bounds);
}

-(NSInteger)numberOfItems{
    //Will be replaced with dynamic value later
    return 5;
}


-(CGSize)collectionViewContentSize{

    CGFloat dragOffset = 180;



    return CGSizeMake([self height], [self width]);
}


-(void)prepareLayout{

    [super prepareLayout];
    cache = [[NSMutableArray alloc]initWithObjects:[UICollectionViewLayoutAttributes class], nil];

    self.standardHeight = 100;
    self.featuredHeight = 280;

    CGRect frame = CGRectZero;
    CGFloat y = 0;

    for(int item = 0; item < 5; item ++){
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:item inSection:0];
        UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];

        attributes.zIndex = item;
        CGFloat height = self.standardHeight;

        if(indexPath.item == [self featuredItemIndex]){
            CGFloat yOffset = self.standardHeight * [self nextItemPercentageOffset];
            y = self.collectionView.contentOffset.y - yOffset;

            height = self.featuredHeight;

    }else if(indexPath.item == ([self featuredItemIndex] + 1) && indexPath.item != [self numberOfItems]){
            CGFloat maxY = y + self.standardHeight;
            height = self.standardHeight + MAX((self.featuredHeight - self.standardHeight) * [self nextItemPercentageOffset], 0);
            y = maxY - height;
    }

        frame = CGRectMake(0, y, [self width], [self height]);
    attributes.frame = frame;
        [cache addObject:attributes];

        y = CGRectGetMaxY(frame);

    }

}

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    [super layoutAttributesForElementsInRect:rect];



    NSMutableArray *layoutAttributes = [[NSMutableArray alloc]initWithObjects:[UICollectionViewLayoutAttributes class], nil];

    NSLog(@"%@", cache);

    for(UICollectionViewLayoutAttributes *attributes in cache){
        if(CGRectIntersectsRect(attributes.frame, rect)){
            [layoutAttributes addObject:attributes];
        }
    }

    return layoutAttributes;

}


-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
    return true;
}

@end

我也遇到错误,由于未捕获的异常而终止应用程序 'NSInvalidArgumentException',原因:'+[UICollectionViewLayoutAttributes 框架]:无法识别的选择器。

我认为错误可能是由于从 Swift 到 Objective-C 的错误翻译,尤其是这一行,

Swift:

var cache = [UICollectionViewLayoutAttributes]()

Objective-C:

NSMutableArray *layoutAttributes = [[NSMutableArray alloc]initWithObjects:[UICollectionViewLayoutAttributes class], nil];

这是我在 Whosebug 上的第一个问题,非常感谢任何反馈和帮助,提前致谢。

您在初始化时将对 UICollectionViewLayoutAttributes class 的引用添加到 cachelayoutAttributes 数组。然后,调用 frame 属性 到 class 引用(注意错误消息中的 + 表示 class 方法,其中实例方法使用 -).

替换此行:

NSMutableArray *layoutAttributes = [[NSMutableArray alloc]initWithObjects:[UICollectionViewLayoutAttributes class], nil];

有了这个:

NSMutableArray *layoutAttributes = [[NSMutableArray alloc] init];

同样适用于cache变量初始化。


您还可以使用泛型在 Objective-C 中使用类型安全数组:

NSMutableArray<UICollectionViewLayoutAttributes*> *layoutAttributes = [[NSMutableArray alloc] init];

更多信息here