UICollectionView添加图像和按钮

时间:2015-02-05 09:04:22

标签: xcode uicollectionview uicollectionviewcell

我使用UICollectionView显示来自CameraRoll的图像,它工作正常,现在我想在collectionview单元格中添加Camera按钮。我写了这样的代码,按钮没有显示。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];

[self.cameraRoll thumbAtIndex:indexPath.row completionHandler:^(UIImage *thumb) {
    UIImageView *imageView = [[UIImageView alloc] initWithImage:thumb];
    imageView.frame = CGRectMake(0,0,cell.frame.size.width, cell.frame.size.height);
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    [cell.contentView addSubview:imageView];

    //Now Create Button
    UIImage* img = [UIImage imageNamed:@"btn_take_pic"];
    UIImageView *btnImage = [[UIImageView alloc] initWithImage:img];
    btnImage.frame = CGRectMake(0,0, 50,50);
    btnImage.frame = CGRectMake(0,0,cell.frame.size.width, cell.frame.size.height);
    btnImage.contentMode = UIViewContentModeScaleAspectFill;
    //End of create button

    [cell.contentView addSubview:btnImage];//Add Button to the cell


}];

return cell;
}

2 个答案:

答案 0 :(得分:2)

像这样创建按钮

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];

//Add your image showing code here

//Now Create Button in cell

    CGRect btnRect = CGRectMake(0, 0 , 30 , 30);
    UIButton *cellBtn = [[UIButton alloc] initWithFrame:btnRect];
    [cellBtn setBackgroundImage:[UIImage imageNamed:@"img.png"] forState:UIControlStateNormal];
    [cellBtn setTitle:@"Text to Show" forState:UIControlStateNormal];
    [cell.contentView addSubview:cellBtn];        

    [[cell cellBtn] addTarget:self action:@selector(CellBtnTapped:event:) forControlEvents:UIControlEventTouchUpInside];

return cell;
}

按钮点击方法

-(void)CellBtnTapped:(id)sender event:(id)event
{
// perform your action here that you want to do on button tap
}

但是你应该为你的CollectionViewCell创建一个单独的类,并在那里添加你想要在collectionView单元中获取的所有对象。

答案 1 :(得分:1)

这是我上面提到的问题的解决方案,我认为它可能对其他人有帮助,所以在这里提出代码

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
   if(indexPath.row>0)
    [self.cameraRoll thumbAtIndex:(indexPath.row-1) completionHandler:^(UIImage *thumb) {
    UIImageView *imageView = [[UIImageView alloc] initWithImage:thumb];
    imageView.frame = CGRectMake(0,0,cell.frame.size.width, cell.frame.size.height);
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    [cell.contentView addSubview:imageView];
}];
else{
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"btn_take_pic.png"]];
    imageView.frame = CGRectMake(0,0,cell.frame.size.width, cell.frame.size.height);
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    [cell.contentView addSubview:imageView];
}
return cell;
}