1

I have created a custom xib and my aim is to display it like the image below in a collection view:

enter image description here

I set up my collection view like this:

Collection View Title

+(id)newTitleView
 {
VideoCollectionViewTitleView* vidcollectionTitleView = [[[NSBundle mainBundle] loadNibNamed:@"ProductCollectionViewTitleView" owner:nil options:nil] lastObject];

if ([vidcollectionTitleView isKindOfClass:[VideoCollectionViewTitleView class]])
{
    return vidcollectionTitleView;
}
else
{
    return nil;
}
}

Data Source

-(instancetype)initWithVideos:(Videos *)videos
  {
    self = [super init];
    if (self)
   {
    _videos = videos;
   }
return self;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{

return _videos.content.count ;

}


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

Video *video = [self.videos content][indexPath.row];
VideoCollectionViewCell *cell = [collectionView  dequeueReusableCellWithReuseIdentifier:kGENERIC_COLLECTION_CELL_ID forIndexPath:indexPath];


for (int cnt = 0; cnt <[[video youtube]count]; cnt++) {

    NSMutableString *youtubeImageString = [[NSMutableString   alloc]initWithString:kYOUTUBE_IMAGE_URL];

    [youtubeImageString replaceOccurrencesOfString:@"videoId" withString: [video youtube][cnt++] options:0 range:NSMakeRange(0, youtubeImageString.length-1)];
    NSLog(@"check the url %@", youtubeImageString);

    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:youtubeImageString]
                      placeholderImage:nil
                             completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                                 if (cacheType != SDImageCacheTypeMemory)
                                 {
                                     cell.imageView.alpha = 0.0;
                                     double val = ((double)arc4random()/ARC4RANDOM_MAX)*0.8;
                                     [UIView animateWithDuration:0.2 delay:val options:UIViewAnimationOptionCurveEaseInOut animations:^{
                                         cell.imageView.alpha = 1.0;
                                     } completion:nil];
                                 }

                             }];

    //set labels

    [cell.nameLabel setText:@"Videoooos"];

}

return cell;
}

This is how I add the objects to the data source:

 VideoCollectionViewTitleViewDataSource *dataSource = [[VideoCollectionViewTitleViewDataSource alloc]initWithVideos:self.model.videos];
        [self.collectionViewDataSourceArray addObject:dataSource];

And this is the result that I am getting:

result

When I put a break point on : [self.collectionViewDataSourceArray addObject:dataSource]; this is what I am getting:

break point

As you can see there is only item but there are more than one item in my array, any suggestions on what I am doing wrong here would be really appreciated as I am stuck on this.

Video.Content[ ]Youtube[ ] :

[type]: videos [content]: ( " \n [youtube]: (\n 4Cw856cbyNI,\n GcmEqI9fpYo,\n L7xR42O825Q,\n kZem01RQOeQ,\n
L7xR42O825Q,\n \"swgU9Q-8aEA\",\n \"v_joMRl6wus\",\n
tKwSgFo0Dz8,\n \"Rg2C-9QJmN0\",\n \"UEW3EYc_aKE\"\n
)\n" )

Videos Class:

    @interface Videos : JSONModel

@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) NSString *type;
@property (strong, nonatomic) NSString *display;
@property (strong, nonatomic) NSArray <Video> *content;

Video Class

#import <Foundation/Foundation.h>
#import "JSONModel.h"

@protocol Video @end
@interface Video : JSONModel

@property (strong, nonatomic) NSArray *youtube;

@end

Note : I have already looked at : Link 1, however it is not useful in my case.

  • What is the value of `_videos.content.count` when `numberOfItemsInSection` is called? – DonMag Sep 14 '17 at 13:21
  • @DonMag "section = 0 " that's what I get when I put a break point there –  Sep 14 '17 at 13:28
  • No... what is the value of `_videos.content.count`? Is it a valid array of somethings? Or is it an array of `1` object? – DonMag Sep 14 '17 at 13:32
  • @DonMag It is an array of object and it inherits the youtube array from a different class –  Sep 14 '17 at 13:35
  • @DonMag I've updated the question –  Sep 14 '17 at 13:37
  • OK - so what is the value???? Your image shows only **ONE** "box" under the Videos title, which indicates the collection view only has **ONE** item in its datasource which indicates `numberOfItemsInSection` is returning `1` ... which means your array contains only **ONE** element. But the only way to know that is to ***check the value of `_videos.content.count`*** – DonMag Sep 14 '17 at 13:39
  • @DonMag the value is the video id of a youtube video = "L7xR42O825Q" –  Sep 14 '17 at 13:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/154453/discussion-between-donmag-and-taha-amini). – DonMag Sep 14 '17 at 13:43

1 Answers1

0

1.let cellReuseIdentifier : String = “cellName”.

2.@IBOutlet weak var myCollectionView: UICollectionView?

3.var nibName = UINib(nibName: cellReuseIdentifier, bundle:nil) myCollectionView?.register(nibName, forCellWithReuseIdentifier: cellReuseIdentifier)

4.let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellReuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell

Afzal Siddiqui
  • 24
  • 2
  • 12