【问题标题】:Error With UICollectionView Sections While Accessing Cells访问单元格时 UICollectionView 部分出错
【发布时间】:2015-07-10 23:08:58
【问题描述】:

我正在编写一个循环,它将遍历我的每个单元格,并使它们摆动。 (如 iOS 主屏幕上的删除效果)。当我只使用一个单元格时,它就像一种魅力。当我编写一个循环来执行此操作时,我收到一个奇怪的错误:

由于未捕获的异常而终止应用程序 'NSInternalInconsistencyException',原因:'请求数量 第 2 节之前的项目,当第 2 节中只有 1 节时 集合视图'

现在,当我研究此错误时,我能找到的唯一信息是当用户尝试删除单元格时发生了这种情况。那不是我的情况。 (好吧,但至少。只是想让它们都先摆动)。

这是我的代码:

#define RADIANS(degrees) ((degrees * M_PI) / 180.0)
    CGAffineTransform leftWobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(-5.0));
    CGAffineTransform rightWobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(5.0));

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer {

  int visible = self.gridView.visibleCells.count;

    for (int i = 0; i < visible; i++) {

    NSIndexPath *path = [NSIndexPath indexPathWithIndex:i];
    myCell = [self.gridView cellForItemAtIndexPath:path];

    myCell.transform = leftWobble;

    [UIView beginAnimations:@"wobble" context:(__bridge void *)(myCell)];
    [UIView setAnimationRepeatAutoreverses:YES]; // important
    [UIView setAnimationRepeatCount:10];
    [UIView setAnimationDuration:1.25];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(wobbleEnded:finished:context:)];

    myCell.transform = rightWobble; // end here & auto-reverse

    }
  [UIView commitAnimations];

}
@end

就像我说的,它适用于一个单元格,但当我尝试编写一个循环来完成所有这些任务时会抛出该错误。有谁知道这里有什么问题?

旁注,如何在此循环中以编程方式在集合视图单元格的左上角添加一个小的删除按钮?我对集合视图非常非常陌生。

还刚刚发现,当我横向滚动时,我的整个视图都在晃动,而不仅仅是单元格......非常奇怪。

【问题讨论】:

    标签: objective-c uikit uicollectionview uicollectionviewcell


    【解决方案1】:

    试试这个:

    #define RADIANS(degrees) ((degrees * M_PI) / 180.0)
    CGAffineTransform leftWobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(-5.0));
    CGAffineTransform rightWobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(5.0));
    
    for (UICollectionView *cell in self.gridView.visibleCells) {
        cell.transform = leftWobble;
    }
    
    [UIView beginAnimations:@"wobble" context:nil];
    [UIView setAnimationRepeatAutoreverses:YES]; // important
    [UIView setAnimationRepeatCount:10];
    [UIView setAnimationDuration:1.25];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(wobbleEnded:finished:context:)];
    
    for (UICollectionViewCell *cell in self.gridView.visibleCells) {
        cell.transform = rightWobble;
    }
    [UIView commitAnimations];
    

    如何在此循环中以编程方式在集合视图单元格的左上角添加一个小的删除按钮?

    将删除按钮作为UIButton 添加到您自定义UICollectionViewCell 子类中的单元格子视图中,并在需要时添加setHidden:NO

    也刚刚发现,当我横向滚动时,我的整个视图都在晃动,而不是 只是细胞……很奇怪。

    已编辑。

    【讨论】:

      猜你喜欢
      • 2016-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-03
      • 1970-01-01
      • 2017-09-26
      相关资源
      最近更新 更多