【问题标题】:UICollectionView reloading not properlyUICollectionView 重新加载不正确
【发布时间】:2015-05-08 09:55:29
【问题描述】:

我有这种要求,如果用户选择了 1,那么我必须通过旋转 90 度来显示图像(在集合视图单元格中),如果用户选择了 4,那么我必须将图像显示为圆形。对于选择 2 和 3,按原样显示图像。
为此,我写了下面的代码。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FilterCell *cell = (FilterCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"filterCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[FilterCell alloc] init];
    }
        if (filterScreen==1) {
            cell.imgView.image = [collectionImage objectAtIndex:indexPath.row];
            cell.imgView.transform = CGAffineTransformMakeRotation(M_PI_2);
        }
        else if (filterScreen==2) {
            cell.imgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"filter_%ld",(long)indexPath.row]];
        }
        else if (filterScreen==3) {
            cell.imgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"motiontitle_contents_%ld",(long)indexPath.row]];
        }
        else if (filterScreen==4) {
            cell.imgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"bgm_%ld",(long)indexPath.row]];
            cell.imgView.layer.cornerRadius = cell.imgView.frame.size.height /2;
            cell.imgView.layer.masksToBounds = YES;
        }
    return cell;
}

在选择 1、2、3 或 4 时,我正在重新加载集合视图。

问题在于,如果用户在选择 1 后选择了 4,则显示后的一些图像会旋转,之后如果用户选择 2 或 3,则一些图像会旋转,而一些图像会旋转。 我曾尝试在重新加载之前删除部分here,但应用程序崩溃和日志是

*** -[UICollectionView _endItemAnimations] 中的断言失败,/SourceCache/UIKit/UIKit-3318.93/UICollectionView.m:3917

重载代码如下:

NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,0)];
[filterCollectionView deleteSections:indexSet];
[filterCollectionView reloadData];

更新: 如果尝试使用
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,1)];
然后得到

*** -[UICollectionView _endItemAnimations] 中的断言失败,/SourceCache/UIKit/UIKit-3318.93/UICollectionView.m:3901

谁能帮我解决这个问题?

【问题讨论】:

  • 请包含断言失败。

标签: ios objective-c uicollectionview uicollectionviewcell


【解决方案1】:

好的,我花了一段时间才明白你的问题。

首先,您尝试的解决方案是错误的。您想重新加载一个部分,但改为删除它(尝试reloadSections)。

但是,这也不能解决您的问题。单元格被重复使用,并且您永远不会重置 transform 属性。试试这个:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FilterCell *cell = (FilterCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"filterCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[FilterCell alloc] init];
    }
    cell.imgView.transform = CGAffineTransformIdentity;
    cell.imgView.layer.cornerRadius = 0;
    cell.imgView.layer.masksToBounds = NO;

        if (filterScreen==1) {
            cell.imgView.image = [collectionImage objectAtIndex:indexPath.row];
            cell.imgView.transform = CGAffineTransformMakeRotation(M_PI_2);
        }
        else if (filterScreen==2) {
            cell.imgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"filter_%ld",(long)indexPath.row]];
        }
        else if (filterScreen==3) {
            cell.imgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"motiontitle_contents_%ld",(long)indexPath.row]];
        }
        else if (filterScreen==4) {
            cell.imgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"bgm_%ld",(long)indexPath.row]];
            cell.imgView.layer.cornerRadius = cell.imgView.frame.size.height /2;
            cell.imgView.layer.masksToBounds = YES;
        }
    return cell;
}

【讨论】:

  • 是的,它有效。我读到某处重新加载collectionview有时行为不端所以尝试先删除部分然后重新加载。但我不知道如何重置转换和图层属性...谢谢....
  • 如何防止重复使用单元格?比如,如果我想要一些新鲜的细胞。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多