UITableViewCell中的UIButton出现问题

3
在我的应用程序中,我正在使用自定义表格。每个单元格都有一个uibutton和uiimage。当按钮发生触摸事件时,我想调用uiimagepickercontroller方法从iphone库中选择一张图片并在图像视图中显示它。我已经编写了代码,但是收到了一个警告……'customCell'可能无法响应presentmodalviewcontroller animated...这里的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   
}  

这是自定义单元格类.. 而且视图控制器类有...

- (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;
 } 
2个回答

6

UITableViewCell不响应-presentModalViewController:animated:方法。

你可以为自定义单元格添加指向视图控制器的指针,然后在视图控制器上调用-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完成了它。从库中导入图像到每个单元格的ImageView中。但是新问题是,当向下滚动表格时,它们会消失... - Nithin
我需要在cellForRowAtIndexPath方法中更改任何内容吗? - Nithin

0
这并不能回答你的问题,但如果你必须在单元格中使用UIButton,那么你可能正在错误地设计你的UI。尝试想出一种让整个单元格执行操作的方法,或者如果不行,使用披露小部件或其他东西。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接