iphone - 覆盖 layoutAttributesForElementsInRect : of UICollectionView/PSTCollectionView causes visible cell to reload

标签 iphone ios uicollectionview sony pstcollectionview

我想在 iPhone 应用程序中模仿 Sony Xperia 中提供的图库应用程序的功能。我的画廊应用程序,图像按日期分组显示在网格中,该部分的第一张照片是其他照片的两倍。放大/缩小时,所有照片都会缩小/放大。

enter image description here

我按照 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;
}

这行得通,布局看起来就像我想要的那样。 通过这种方法,比使用默认布局的单元格更多的单元格变得可见,我的布局看起来不错。但是当我向下滚动时,一些可见的单元格正在重新加载。我想原因是委托(delegate)方法 - (PSUICollectionViewCell *)collectionView:(PSUICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath- (NSArray *)layoutAttributesForElementsInRect:(CGRect )rect 在我的自定义布局中。因此,在默认布局中不可见但在自定义布局中可见的单元格会延迟重新加载。

我怎样才能克服这个问题?

最佳答案

使用UICollectionView 如果应用程序应该支持低于 5.0 使用 PSTCollectionView

A nice startup

关于iphone - 覆盖 layoutAttributesForElementsInRect : of UICollectionView/PSTCollectionView causes visible cell to reload,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17917400/

相关文章:

iphone - [MyClass alloc] 和 [[self class] alloc] 之间的区别在哪里?

ios - Swift 2 .plist dictArray 到远程 Json 数组错误

ios - 在“设置”应用程序中显示 iOS 应用程序内容的删除选项

ios - UICollectionViews GetSizeForItem() 在调用 collectionView.DequeueReusableCell () 时崩溃

swift - UICollectionView.setCollectionViewLayout 出现 EXC_BAD_ACCESS 错误

iOS 位置服务警报 - 显示它之间的延迟是多少?

iphone - 动态调整 tableView 单元格的高度

ios - reloadItemsAtIndexPaths 上的 Collection View 崩溃

iphone - 如何在大型中央调度操作中运行异步操作?

ios - UINavigationController 框架问题