【问题标题】:Overriding layoutAttributesForElementsInRect: of UICollectionView / PSTCollectionView causes visible cell to reload覆盖 UICollectionView / PSTCollectionView 的 layoutAttributesForElementsInRect: 导致可见单元格重新加载
【发布时间】: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


【解决方案1】:

使用UICollectionView 如果应用应该支持低于 5.0 的版本,请使用 PSTCollectionView

A nice startup

【讨论】:

  • 感谢您的回复。我浏览了教程,它看起来很有希望。只需一个查询,可以使用 UICollectionView / PSTCollectionView 使用捏合手势放大/缩小图像吗?
  • 我认为它们不存在但可以包含。它实际上带有tap的方法,但是可以设置pinch
  • 谢谢。我的意思是,如果重新加载时它可能会闪烁,它可以在不闪烁集合视图的情况下顺利完成。
  • 不闪烁..我没试过,但可能有办法不重新加载
  • 您好,感谢您的建议。我将 PSTCollectionView 与自定义布局一起使用,该布局对我有用,并且布局看起来也完全符合需要。但是当细胞重新加载时,这会引发一个问题。有关详细信息,请参阅我编辑的问题。
猜你喜欢
  • 2014-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-31
  • 2017-01-01
  • 1970-01-01
  • 2013-10-08
  • 1970-01-01
相关资源
最近更新 更多