96、collectionViewCell防止复用的两种方法

发布时间 2023-12-04 16:28:00作者: 强者VS弱者
第一种:
//在创建collectionView的时候注册cell(一个分区) UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@“cell" forIndexPath:indexPath]; for (UIView *view in cell.contentView.subviews) { [view removeFromSuperview]; }
第二种:
//在cellForItem方法中注册cell(多个分区)

 NSString *identifier=[NSString stringWithFormat:@"%ld%ld",(long)indexPath.section,(long)indexPath.row];
  [collect registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:identifier];
  UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
  for(id subView in cell.contentView.subviews){
    if(subView){
      [subView removeFromSuperview];
    }
  }