UICollectionView 重新加载不正确
UICollectionView reloading not properly
我有这样的要求,如果用户选择了 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 重新加载之前删除部分,但应用程序崩溃并且日志为
*** Assertion failure in -[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)];
然后得到
*** Assertion failure in -[UICollectionView _endItemAnimations], /SourceCache/UIKit/UIKit-3318.93/UICollectionView.m:3901
谁能帮我解决这个问题?
好的,我花了一段时间才理解你的问题。
首先,您尝试的解决方案是错误的。您想重新加载一个部分,但改为删除它(尝试 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;
}
我有这样的要求,如果用户选择了 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 重新加载之前删除部分,但应用程序崩溃并且日志为
*** Assertion failure in -[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)];
然后得到
*** Assertion failure in -[UICollectionView _endItemAnimations], /SourceCache/UIKit/UIKit-3318.93/UICollectionView.m:3901
谁能帮我解决这个问题?
好的,我花了一段时间才理解你的问题。
首先,您尝试的解决方案是错误的。您想重新加载一个部分,但改为删除它(尝试 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;
}