Preparación CollectionView básico
1) Añadir UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
2) en ViewDidLoad:
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
self.collectionView.register(UINib(nibName: "ShowCell", bundle: nil), forCellWithReuseIdentifier: "ShowCell")
3)
//MARK: CollectionView
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.categories.count;
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MainPictoCell", for: indexPath) as! MainPictoCell
let tag = self.categories[indexPath.row];
if(tag.picture != nil){
let link = URL(string:(tag.picture!.path!));
if (link?.absoluteString.hasPrefix("http"))! {
cell.imgPicto.sd_setImage(with: link, placeholderImage: #imageLiteral(resourceName: "placeholder"));
} else {
do {
let path = ApplicationModel.sharedInstance.picturesDirectoryPath.appendingPathComponent((link?.absoluteString)!);
let imageData = try Data(contentsOf: path)
cell.imgPicto.image = UIImage(data: imageData);
} catch {
print("Error loading image : \(error)")
}
}
}
else{
cell.imgPicto.image = #imageLiteral(resourceName: "placeholder");
}
if (tag.crossed) {
cell.imgCross.isHidden = false;
} else {
cell.imgCross.isHidden = true;
}
let (name, translated) = tag.getName();
cell.lblName.text = name.uppercased();
if(translated){
cell.lblName.textColor = UIColor.white;
}
else{
cell.lblName.textColor = UIColor.red;
}
cell.tag = indexPath.row;
let tagTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(goTags(sender:)))
cell.isUserInteractionEnabled = true;
cell.addGestureRecognizer(tagTap)
return cell;
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//selectedCategory = self.categories[indexPath.row];
// self.performSegue(withIdentifier: "showTagDetail", sender: self);
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let dim = MainController.calculateImageSize(width:collectionView.bounds.width);
return CGSize(width: dim, height: dim + CGFloat(AppConstants.Viewer.LABEL_SIZE))
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return CGFloat(2.0);
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return CGFloat(2.0);
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(0.0, AppConstants.Viewer.SPACE_BETWEEN_CELLS, 0.0, AppConstants.Viewer.SPACE_BETWEEN_CELLS);
}0) La view que se incrustará, se pone como view dentro de un XIB collectionViewCell
1) en el .h de la clase indicar el delegate y datasource
@interface RTShowsController : RTViewController <UICollectionViewDelegate, UICollectionViewDataSource>
2) en viewDidLoad asignar:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//Asignamos DataSource y Delegate
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
//Asociamos el XIB que incrustaremos
[self.collectionView registerClass:RTShowCell.class forCellWithReuseIdentifier:@"RTShowCell"];
[self.collectionView registerNib:[UINib nibWithNibName:@"RTShowCell" bundle:nil] forCellWithReuseIdentifier:@"RTShowCell"];
}
3) Implementar métodos de sección
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [self.showsList count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"RTShowCell";
RTShowCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
RTShow* show = [self.showsList objectAtIndex:indexPath.row];
NSURL* imagePath = [NSURL URLWithString:show.showThumbnail];
dispatch_async(dispatch_get_main_queue(), ^{
[cell.vwContent.imgShow loadImageFromURL:[imagePath absoluteString] andPlaceholder:[UIImage imageNamed:DEFAULT_IMAGE]];
});
cell.vwContent.lblSchedule.text = show.showTime;
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%f",self.view.frame.size.width);
return CGSizeMake((self.view.frame.size.width / 2) -1, (self.view.frame.size.width / 2));
}
#pragma mark collection view cell paddings
- (UIEdgeInsets)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(0, 0, 0, 0); // top, left, bottom, right
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 0;
}
- (CGFloat)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 2;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
selectedProgram = (int) indexPath.row;
[self performSegueWithIdentifier:@"showProgram" sender:self];
}