gpt4 book ai didi

ios - UICollectionViewLayout(从 Swift 转换为 Objective-C)

转载 作者:搜寻专家 更新时间:2023-11-01 07:26:15 29 4
gpt4 key购买 nike

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

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

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

以下是我从 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];

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

最佳答案

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

替换这一行:

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

有了这个:

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

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


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

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

更多信息 here .

关于ios - UICollectionViewLayout(从 Swift 转换为 Objective-C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35994285/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com