2

我有一个CollectionViewController和一个CollectionViewCell。我从数据库中获取数据,所以当控制器加载时,它会动态地创建相应的单元格。UIImagePickerController和CollectionView控制器/单元格

每个单元格都有一个UIButton和UITextView。 我正在使用UIButton来显示图片(如果它存在于数据库中)或捕获图像(如果按下)。

InboundCollectionViewController.m 

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

    Image *current = [images objectAtIndex:indexPath.row]; 

    [inboundDetailCell.imageType setText:[NSString stringWithFormat:@"%@", [current pd_description]]]; 

    if ([current.pd_image isKindOfClass:[NSData class]] == NO) { 
     [inboundDetailCell.imageButton addTarget:self action:@selector(useCamera)  forControlEvents:UIControlEventTouchUpInside]; 
    } 
    else { 
     [inboundDetailCell.imageButton setImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal]; 
    } 

    return inboundDetailCell; 
} 

到目前为止,这么好。我启动我的应用程序。集合视图控制器根据数据库的结果填充单元格。

如果像场有一个图像,然后在“check.png”被加载在我的自定义的ImageButton的图像性能。

如果图像字段不具有的图像,则对于所述的ImageButton动作TouchUpInside被设定为方法“useCamera”。现在

- (void)useCamera 
{ 
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; 

    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 
    { 
     [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera]; 
    } 
    else 
    { 
     [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; 
    } 

    [imagePicker setDelegate:self]; 
    [self presentViewController:imagePicker animated:YES completion:NULL]; 
} 

,按照我下面的教程中,我必须执行以下代码:

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; 

    // set image property of imageButton equal to the value in UIImage 'image' variable ??? 

    [self dismissViewControllerAnimated:YES completion:NULL]; 
} 

在我发现大多数例子中,ImageView的和ImagePickerController在同一视图控制器内创建。因此,访问ImageView的图像属性(或我的情况下的按钮)很容易。

我的问题是我的 'IBOutlet中的UIButton的ImageButton' 位于内InboundCollectionViewCell,不InboundCollectionViewController。所以,我无法找到一种方法将相机返回的图像传递给我的按钮的图像属性。

请注意,我是很新的客观C和Xcode中,这是我的第一个项目。所以要温柔! :P :)

预先感谢您!

回答

1

确保useCamera接收一个已经按下了按钮,存储在一个成员变量:

- (void)useCamera:(id)sender { 
    UIButton *button = (UIButton *)sender; 
    self.lastButtonPressed = sender; // A member variable 

    ... 
} 

请注意,您需要自签名已改为touchUpInside重新映射到这个功能。现在

,在imagePickerController:didFinishPickingMediaWithInfo:您可以访问self.lastButtonPressed更新其图像的成员变量。

Tim

+0

我设置按钮的动作,但我不知道如何通过这种方式传递参数。 '[cell.imageButton addTarget:自动作:@selector(useCameraSingle)forControlEvents:UIControlEventTouchUpInside];' –

相关问题