collectionView一些注意的地方

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值