2009-12-02 107 views
3

在我的應用程序中,我使用了一個自定義表格。每個細胞都有一個uibutton和uiimage。當按鈕碰觸時,我想調用uiimagepickercontroller方法從iphone庫中選擇一張圖片並將其顯示在圖像視圖中。我寫了它,但得到了一個警告......'customCell'可能不會響應presentmodalviewcontroller的動畫...這裏customCell是我的主類myApp的子類,也是自定義單元格的筆尖的名稱。任何人都知道的問題? 謝謝...UItableViewCell中的uibutton問題

編輯

- (IBAction)selectExistingPicture1 { 
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary]) { 
     UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
     picker.delegate = self; 
     picker.allowsImageEditing = YES; 
     picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
     [self presentModalViewController:picker animated:YES]; 
     [picker release]; 
    } 
    else { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error accessing photo library" message:@"Device does not support a photo library" delegate:nil cancelButtonTitle:@"Drat!" otherButtonTitles:nil]; 
     [alert show]; 
     [alert release]; 
    } 
} 

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {  
    CGSize newSize = CGSizeMake(80, 80); 
    UIGraphicsBeginImageContext(newSize); 
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; 
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    imageView.image = newImage; 

    [picker dismissModalViewControllerAnimated:YES]; //warning shown here 
} 

這是自定義單元格類..和的viewController類有...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CustomCellIdentifier = @"CustomCellIdentifier"; 
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier]; 

    if (cell == nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil]; 
     for (id currentObject in nib){ 
      if ([currentObject isKindOfClass:[CustomCell class]]){ 
       cell = (CustomCell *)currentObject; break; 
      } 
     } 
    } 

    NSUInteger s= indexPath.section; 
    //[cell setText:[NSString stringWithFormat:@"I am cell %d", indexPath.row]]; 

    NSUInteger r = indexPath.row; 
     cell.imageView.image = nil; 
    for (s;s==0;s++) 
    for(r;r==0;r++) 
    {  
      UIImage *img=imageView.image; 
     cell.imageView.image = img; 
     } 
    return cell; 
} 

回答

6

UITableViewCell並不-presentModalViewController:animated:迴應。

你可能會給你的CustomCell一個指向你的視圖控制器的指針,然後在視圖控制器上調用-presentModelViewController:animated:

一個實例變量添加到您的自定義單元格類:

@interface CustomCell : UITableViewCell { 
    UIViewController *viewController; 
} 
@property (nonatomic, assign) UIViewController *viewController; 
@end 

-tableView:cellForRowAtIndexPath:,創建一個新的CustomCell後,設置屬性:

if (cell == nil) { 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil]; 
    for (id currentObject in nib){ 
     if ([currentObject isKindOfClass:[CustomCell class]]){ 
      cell = (CustomCell *)currentObject; 
      cell.viewController = self; // <-- add this 
      break; 
     } 
    } 
} 

然後,在你CustomCell類,取代

[self presentModalViewController:picker animated:YES]; 

[self.viewController presentModalViewController:picker animated:YES]; 
+0

我用單元格中的uibutton完成了它..從庫導入的圖像到每個單元格的圖像視圖。但新的問題是,他們消失在滾動下表... – Nithin 2009-12-02 08:11:39

+0

我必須在cellForRowAtIndexPath:方法? – Nithin 2009-12-02 08:33:50

+0

謝謝老闆。 @Thomas – Ramdy 2014-02-28 05:51:28

0

這不會回答你的問題,但如果你必須在單元格中有UIButton,那麼你的UI設計可能會錯誤。試着想辦法讓整個細胞執行行動,如果不是這樣,使用披露小工具或其他東西。