iOS:UICollectionViewCell的完整帧不是可点击/可点击的

时间:2015-02-20 13:25:11

标签: ios uicollectionviewcell

我使用UIView创建了UICollectionView,并在UIView上添加了UIViewControllerUIViewController使用photoDidTapped的委托(UIView)来确定点击/点按的内容。第一行,没关系,意味着整个单元区域是可点击的。第二行,水平一半是可点击的。第3行,几乎10%的单元格水平可点击,依此类推。

代码:

- (UICollectionView *)collectionView
{

    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = CGSizeMake(THUMB_DIMENSION, THUMB_DIMENSION);
        [flowLayout setMinimumInteritemSpacing:0.0f];
        [flowLayout setMinimumLineSpacing:PHOTO_MARGIN];

        _collectionView = [[UICollectionView alloc] initWithFrame:self.frame collectionViewLayout:flowLayout];
        _collectionView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.frame.size.height);
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.allowsSelection = YES;
        _collectionView.alwaysBounceVertical = YES;
        _collectionView.scrollEnabled = NO;

        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];


    }

    return _collectionView;
}


- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
    return (_photos.count > 9 ? 9 : _photos.count);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath; {

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];

    imgView.clipsToBounds = YES;
    imgView.backgroundColor  = RGB(250, 250, 250);


    if (_photos.count) {
        PhotoDM *photo = [_photos objectAtIndex:indexPath.row];
        [imgView setImageWithURL:[NSURL URLWithString:photo.largeLink]
                placeholderImage:[UIImage imageNamed:@"placeholder"]];
    }

    [cell addSubview:imgView];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"tapped: %ld", (long)indexPath.row);

    if([delegate respondsToSelector:@selector(photoDidTapped:)])
        [delegate performSelector:@selector(photoDidTapped:) withObject:[[NSNumber alloc] initWithInteger:indexPath.row] ];
}

1 个答案:

答案 0 :(得分:0)

您的代码包含2个问题 1.如果您创建子视图,则应使用self.bounds 2.添加UICollectionView后,可以调整视图大小。这意味着您应该配置autoresizingMaskNSLayoutConstrans。 (UICollectionView可以超出视图范围)。

要调试此问题,您可以为视图,collectionView,viewController的视图创建不同的背景颜色。