0


D'abord, désolé pour ma longue question.
Je crée un UITableViewCell personnalisé avec UICollectionView à l'intérieur. Chaque fois que les utilisateurs sélectionnent cette cellule, UIImagePickerController apparaîtra, et maintenant je veux passer toutes les images sélectionnées de UIImagePickerController dans UICollectionView dans ma cellule personnalisée.
J'ai été complété pour afficher le sélecteur d'image et maintenant mon problème est d'afficher les images sélectionnées dans la vue de collection.
Comment puis-je le faire?
Mon code:
Ajouter des images de UIImagePickerViewController à UICollectionView à l'intérieur de UITableViewCell

SHImageUploadCell.h

#import <UIKit/UIKit.h> 
@class SHImageUploadCell; 
@protocol SHImagUploadDelegate<NSObject> 
@optional 
- (void)closeImage:(SHImageUploadCell*)cell; 
@end 
@interface SHImageUploadCell : UICollectionViewCell 
@property (weak, nonatomic) IBOutlet UIImageView *imgUpload; 
@property (strong,nonatomic) id<SHImagUploadDelegate> delegate; 
@property(nonatomic) int imageIndex; 
@property (weak, nonatomic) IBOutlet UIImageView *bugImage; 

@end 

SHImageUploadCell.m

#import "SHImageUploadCell.h" 

@implementation SHImageUploadCell 
- (IBAction)bntDeleteImageClick:(id)sender { 


    NSDictionary* userInfo = @{@"index": @(self.imageIndex)}; 

    NSLog(@"%d", self.imageIndex); 
    if (_delegate) { 
     [_delegate closeImage:self]; 
    } 
} 

@end 

MyCustomCell.h

#import <UIKit/UIKit.h> 

@interface SHPhotoPickerViewCell : UITableViewCell 
@property (weak, nonatomic) IBOutlet UICollectionView *photoCollection; 
@property (strong, nonatomic) NSMutableArray *listImage; 
@end 

MyCustomCell.m

#import "SHPhotoPickerViewCell.h" 
#import "SHImageUploadCell.h" 

@interface SHPhotoPickerViewCell()<UICollectionViewDataSource, UICollectionViewDelegate, SHImagUploadDelegate> 

@end 

@implementation SHPhotoPickerViewCell 

- (void)awakeFromNib { 
    [super awakeFromNib]; 
    // Initialization code 
    [self.photoCollection registerClass:[SHImageUploadCell class] forCellWithReuseIdentifier:@"selectedCell"]; 
    [self.photoCollection registerNib:[UINib nibWithNibName:@"SHImageUploadCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"selectedCell"]; 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated { 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ 
    return self.listImage.count; 
} 

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
    SHImageUploadCell *uploadCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"uploadCell" forIndexPath:indexPath]; 
    uploadCell.imgUpload.image = [UIImage imageWithData: self.listImage[indexPath.row]]; 
    uploadCell.imageIndex = (int)indexPath.row; 
    uploadCell.delegate=self; 
    return uploadCell; 
} 

@end 

sélectionnez la cellule de poignée:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
if (indexPath.row == 4){ 
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; 
    imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
    imagePickerController.delegate = self; 
    [self presentViewController:imagePickerController animated:YES completion:nil]; 
    } 
    } 
} 

délégué UIImagePickerController:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    SHPhotoPickerViewCell *photoPickerCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:self.pickerCellIndexPath.row inSection:1]]; 
    UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage]; 
    image = [SHHelper resizeImageWithImage:image toSize:CGSizeMake(480, 320)]; 
    NSData *imageData = UIImageJPEGRepresentation(image, 0.7); 
    photoPickerCell.listImage = [NSMutableArray new]; 
    [photoPickerCell.listImage addObject:imageData]; 

    [photoPickerCell.photoCollection reloadData]; 

    [picker dismissViewControllerAnimated:YES completion:^{ 

    }]; 


} 
+1

vous devez ajouter url dans self.listImage puis recharger votre tableview et collectionview – KKRocks

+0

vous devez s avez-vous cette image? – KKRocks

+0

Avez-vous défini collectionview 'datasource' et' delegate' à tableViewCell? – Rishab

Répondre

0

Essayez ceci:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 

    id object = [listImage objectAtIndex:indexPath.row]; 
    if ([object isKindOfClass:[UIImage class]]) { 
     uploadCell.imgUpload.image = (UIImage *)object; 
    } 
    else{ 
     uploadCell.imgUpload.image = [UIImage imageWithData: object]; 
    } 

     return uploadCell; 
} 

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage]; 
    [listImage addObject:image]; // here add image which you selected 
    // reload tableview 
    // reload collection view 
    [picker dismissViewControllerAnimated:YES completion:^{ 

    }]; 


} 
+0

Rien ne change. Il plante toujours lorsque je suis sélectionné image: '[UITableViewCell listImage]: sélecteur non reconnu envoyé à l'instance 0x7fcd3b897c00' –

+0

dans quelle ligne votre code est brisé? – KKRocks

+0

À '[photoPickerCell.photoCollection reloadData];' ligne –