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];' –

相關問題