【发布时间】:2013-07-29 06:34:51
【问题描述】:
我想在 iPhone 应用中模仿 Sony Xperia 中提供的画廊应用的功能。我的画廊应用程序,图像显示在按日期分组的网格中,该部分的第一张照片的大小是其他照片的两倍。捏合时,所有照片都会缩小/缩小。
我按照 Lithu T.V 的建议使用 PSTCollectionView 并创建了自定义布局。在那个布局中,我已经覆盖了- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect。下面是相同的代码。
// called continuously as the rect changes
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
NSArray *attribs = [super layoutAttributesForElementsInRect:rect];
NSMutableArray *arrmFrames=nil;
for (int i=0;i<attribs.count;i++) {
UICollectionViewLayoutAttributes *attributesInitial=[attribs objectAtIndex:0];
UICollectionViewLayoutAttributes *attributes=[attribs objectAtIndex:i];
//Take initial frame from first cell
if(i==0)
fFirstCellsY = attributes.frame.origin.y;
//while Y is constant, save the adjusted frames for next cells
else if(attributes.frame.origin.y<fFirstCellsY+attributesInitial.frame.size.height)
{
if(arrmFrames==nil)
arrmFrames=[[NSMutableArray alloc]init];
attributes.frame=CGRectMake(attributes.frame.origin.x, attributesInitial.frame.origin.y, attributes.frame.size.width, attributes.frame.size.height);
[arrmFrames addObject:NSStringFromCGRect(CGRectMake(attributes.frame.origin.x, attributes.frame.origin.y+attributes.frame.size.height+10, attributes.frame.size.width, attributes.frame.size.height))];
}
//Adjust the frame of other cells
else
{
CGRect frame = attributes.frame;
attributes.frame=CGRectFromString((NSString*)[arrmFrames objectAtIndex:0]);
[arrmFrames removeObjectAtIndex:0];
[arrmFrames addObject:NSStringFromCGRect(frame)];
}
}
}
return attribs;
}
这行得通,布局看起来就像我想要的那样。
通过这种方法,可以看到比使用默认布局更多的单元格,并且我的布局看起来不错。但是当我向下滚动时,一些可见的单元格正在重新加载。我猜原因是委托方法- (PSUICollectionViewCell *)collectionView:(PSUICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath 在- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect 之前被调用,这在我的自定义布局中。因此在默认布局中不可见但在自定义布局中可见的单元格会延迟重新加载。
我该如何克服这个问题?
【问题讨论】:
-
参考本教程..raywenderlich.com/22324/…
-
@Kalpesh:谢谢。我不能使用 UICollectionView,因为该应用程序应该能够在 iOS 5 上运行。通过 PSTCollectionView
标签: iphone ios uicollectionview sony pstcollectionview