collectionView一些注意的地方

金飞翼
2023-12-01
collectionView必须实现两个方法;
<pre name="code" class="objc">@required

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;

 
 
//每个UICollectionView展示的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)cView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    // must be dequeueReusableCellWithReuseIdentifier !!!!取出cell
    ShowImageCell *cell = (ShowImageCell *)[cView dequeueReusableCellWithReuseIdentifier:KCellIdentifier
                                                                            forIndexPath:indexPath];
    if (!cell) {
        return nil;
    }
//    图片
    NSMutableString * str = [[[_myArr objectAtIndex:indexPath.row] objectForKey:@"images"] objectForKey:@"small"];
    NSString * urlPath = [str stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
    [cell.getImageWithUrl GetImageWithUrl:str directory:@"shadowingImage" name:urlPath finishBlock:^(NSMutableData *data ){
        [cell.ImageView setImage:[UIImage imageWithData:data]];
    }];
//电影名称
 cell.TitleLabel.text = [[_myArr objectAtIndex:indexPath.row] objectForKey:@"title"];
//    评分
    cell.score.text = [[_myArr objectAtIndex:indexPath.row] objectForKey:@"rating"];
      return cell;
}
//没加载出图片使用的方法
-(void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    ShowImageCell * cell1 = (ShowImageCell *)cell;
    cell1.ImageView.image = nil;
    [cell1.getImageWithUrl stopConnection];
    NSLog(@"====");
    
//    [cell]
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
//    UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
//    NSLog(@"collection ^^^^^^^^^^^^^^%d",indexPath.row);
  
    MoveDetailsViewController * movedetails = [[MoveDetailsViewController alloc] init];
    movedetails.movieId =  [[_myArr objectAtIndex:indexPath.row] objectForKey:@"id"];
    NSLog(@"888888888888888888888888888888888------%@-------888888888888888",movedetails.movieId);
    [self.navigationController pushViewController:movedetails animated:YES];
    
}
返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

UICollectionViewLayout设置成3D效果,这里像第三方作者致敬,此处只贴自用那段代码
#pragma mark - UICollectionViewLayout
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //3D代码
    UICollectionViewLayoutAttributes* attributes = attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    UICollectionView *collection = self.collectionView;
    float width = collection.frame.size.width;
    float x = collection.contentOffset.x;
//    设置旋转体多角
    CGFloat arc = M_PI * 2.0f;
    

    int numberOfVisibleItems = [self.collectionView numberOfItemsInSection:0 ];
//  设置图距离左右上下的距离
    attributes.center = CGPointMake(x+160,240.0f);
//    设置框大小
    attributes.size = CGSizeMake(100.0f, 150.0f);

    
    CATransform3D transform = CATransform3DIdentity;
//    设置偏角
    transform.m34 = -1.6f/700.0f;
//改每个图中间的间隔哟
    CGFloat radius = attributes.size.width/1.85/ tanf(arc/2.0f/numberOfVisibleItems);
    CGFloat angle = (indexPath.row-x/width+1)/ numberOfVisibleItems * arc;
//    图片上下左右排版
    transform = CATransform3DRotate(transform, angle, 0.0f, 1.0f, 0.0f);
    transform = CATransform3DTranslate(transform, 0.0f, 0.0f, radius);
    attributes.transform3D = transform ;

    
    
    return attributes;
}


 类似资料: