Проблема с uibutton в UItableViewCell - PullRequest
3 голосов
/ 02 декабря 2009

В моем приложении я использую настроенную таблицу. Каждая клетка имеет uibutton и uiimage. Когда на кнопке происходит прикосновение, я хочу вызвать метод uiimagepickercontroller, чтобы выбрать изображение из библиотеки iphone и отобразить его в представлении изображения. Я написал это, но получаю предупреждение ... 'customCell' может не отвечать на анимированный presentmodalviewcontroller ... здесь customCell - это подкласс моего основного класса, myApp, также имя пера пользовательской ячейки. Кто-нибудь знает проблему ???? Спасибо ...

EDIT

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

Ответы [ 2 ]

6 голосов
/ 02 декабря 2009

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 голосов
/ 02 декабря 2009

Это не ответит на ваш вопрос, но если вам нужно иметь UIButton в ячейке, вы, вероятно, ошибаетесь в своем дизайне пользовательского интерфейса. Попробуйте придумать, как заставить всю ячейку выполнить действие, если не так, то использовать гаджет раскрытия информации или что-то в этом роде.

...