【发布时间】:2016-03-14 17:33:52
【问题描述】:
我目前正在关注创建自定义 UICollectionViewLayout 的本教程 (https://www.raywenderlich.com/99087/swift-expanding-cells-ios-collection-views),问题是本教程是用 Swift 编写的,但我正在尝试将其转换为我的项目的 Objective-C。
将教程复制到 Objective-C 的第一部分很好,我已经到了这个阶段。
但是,在第二部分中,当我们假设创建自定义 UICollectionViewLayout 时,在情节提要中将 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 frame]:无法识别的选择器。
我认为错误可能是由于从 Swift 到 Objective-C 的不正确翻译,尤其是这一行,
斯威夫特:
var cache = [UICollectionViewLayoutAttributes]()
目标-C:
NSMutableArray *layoutAttributes = [[NSMutableArray alloc]initWithObjects:[UICollectionViewLayoutAttributes class], nil];
这是我在 StackOverflow 上的第一个问题,任何反馈和帮助将不胜感激,在此先感谢。
【问题讨论】:
标签: ios objective-c swift uicollectionview uicollectionviewlayout