现在好多学生使用UICollectionView来呈现视图展示,但是在使用UICollectionReusableView来做分区效果的时候,很容易造成重影效果,其造成重影的原因是,在重用的UICollectionReusableView上面一直添加视图,而重用的UICollectionReusableView在重用时,又重新创建了一个,而原来的还在。这个时候就会造成重影效果。先给大家看一下错误的代码:
正确的代码
[Objective-C]
纯文本查看
复制代码
01
02
03
04
05
06
07
08
09
10
|
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(
NSString
*)kind atIndexPath:(
NSIndexPath
*)indexPath
{
UICollectionReusableView * reusable = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:
@"header"
forIndexPath:indexPath];
UILabel * titLabel = [[UILabel alloc]initWithFrame:CGRectMake(35, 15, 200, 30)];
[reusable addSubview:titLabel];
titLabel.text = [_headerArr objectAtIndex:indexPath.section];
[titLabel release];
return
reusable;
}
|
正确的代码
[Objective-C]
纯文本查看
复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
|
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(
NSString
*)kind atIndexPath:(
NSIndexPath
*)indexPath
{
UICollectionReusableView * reusable = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:
@"header"
forIndexPath:indexPath];
UILabel * titLabel = (UILabel *)[reusable viewWithTag:1000];
if
(!titLabel)
{
titLabel = [[UILabel alloc]initWithFrame:CGRectMake(35, 15, 200, 30)];
titLabel.tag=1000;
}
[reusable addSubview:titLabel];
titLabel.text = [_headerArr objectAtIndex:indexPath.section];
return
reusable;
}
|