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:^{
}];
}
vous devez ajouter url dans self.listImage puis recharger votre tableview et collectionview – KKRocks
vous devez s avez-vous cette image? – KKRocks
Avez-vous défini collectionview 'datasource' et' delegate' à tableViewCell? – Rishab