【问题标题】:Problem with uibutton in UItableViewCellUItableViewCell中的uibutton问题
【发布时间】:2009-12-02 06:06:54
【问题描述】:

在我的应用程序中,我使用的是自定义表格。每个单元格都有一个 uibutton 和 uiimage。当按钮上出现触摸时,我想调用 uiimagepickercontroller 方法从 iphone 库中选择一张图片并将其显示在图像视图中。我已经写了,但收到警告...'customCell' 可能无法响应 presentmodalviewcontroller 动画...这里 customCell 是我的主类 myApp 的子类,也是自定义单元格的 nib 的名称。有谁知道这个问题??? 谢谢...

编辑

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

【问题讨论】:

    标签: iphone uitableview uiimagepickercontroller


    【解决方案1】:

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

    【讨论】:

    • 我已经用单元格中的 uibutton 完成了它。从库中导入图像到每个单元格的图像视图。但新的问题是它们在向下滚动表格时消失了......
    • 我必须在 cellForRowAtIndexPath: 方法中更改任何内容吗??
    【解决方案2】:

    这不会回答您的问题,但如果您必须在单元格中使用 UIButton,您的 UI 设计可能会出错。尝试想办法让整个单元格执行该操作,如果不是,请使用披露小工具或其他东西。

    【讨论】:

      猜你喜欢
      • 2011-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-31
      • 1970-01-01
      相关资源
      最近更新 更多