Xcode UICollectionView添加图像和按钮

Xcode UICollectionView添加图像和按钮,xcode,uicollectionview,uicollectionviewcell,Xcode,Uicollectionview,Uicollectionviewcell,我使用UICollectionView显示CameraRoll中的图像,它工作正常,现在我想在collectionview单元格中添加Camera按钮。我写的代码是这样的,按钮没有显示出来 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell =

我使用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;
}

创建这样的按钮

- (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单元格中获取的所有对象。

这是我上面提到的问题的解决方案,我认为这可能对其他人有帮助,因此将代码放在这里

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

您没有在单元格中创建任何按钮。你是…??首先我想在手机中显示btn_take_pic图像,然后我想显示所有的相机图像…谢谢Zubair这给了我一些想法。实际上按钮应该在单独的手机中。。。。在您的解决方案中,按钮显示在图像上。@M Zubair Shamshad add target显示错误[[cell cellBtn]无可见界面found@UmaMadhavi
[cellBtn addTarget:self action:@selector(PerformDiAction)for ControlEvents:UIControlEventTouchUpInside]
您也可以像这样使用它。@M Zubair Shamshad工作正常。我正在传递集合视图单元格中的按钮数组,因此,如果我单击一个按钮,它应该会更改背景颜色(仅选定)@UmaMadhavi为您的按钮分配标签,当您必须更改特定按钮的背景颜色时,比较标签,我有一个问题要问您。那是什么->self.cameraRoll?我是初学者,不幸的是我听不懂。请帮助我;)-