0

I am trying to build a uiCollectionView which pages horizontally per data section - the App is for personal trainers and each section is an exercise which has a number of dynamic attributes (speed, km's, time etc).

I have created a custom cell for the attributes and i'm simply at the stage of trying to apply the custom cell to view view, populating a title and adding the scrolling - here is my code -

- (void)loadView

{
//self.prefs = [NSUserDefaults standardUserDefaults];

//self.collectionView.backgroundView = [[UIImageView alloc] initWithImage:
                             //    [UIImage imageNamed:[self.prefs stringForKey:@"BGImageBlah"]]];




self.view = [[UIView alloc]
             initWithFrame:[[UIScreen mainScreen] bounds]
             ];

// Create a flow layout for the collection view that scrolls
// horizontally and has no space between items
UICollectionViewFlowLayout *flowLayout = [
                                          [UICollectionViewFlowLayout alloc] init
                                          ];
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
flowLayout.minimumLineSpacing = 5;
flowLayout.minimumInteritemSpacing = 0;

// Set up the collection view with no scrollbars, paging enabled
// and the delegate and data source set to this view controller
self.collectionView = [[UICollectionView alloc]
                       initWithFrame:self.view.frame
                       collectionViewLayout:flowLayout
                       ];
self.collectionView.showsHorizontalScrollIndicator = NO;
self.collectionView.pagingEnabled = YES;
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.view addSubview:self.collectionView];




}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.


}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (NSInteger)collectionView:(UICollectionView *)collectionView
 numberOfItemsInSection:(NSInteger)section
{
    return 9;
}




- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
              cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // Dequeue a prototype cell and set the label to indicate the page
    WOColView *cell = [collectionView
                    dequeueReusableCellWithReuseIdentifier:@"CellWO"
                    forIndexPath:indexPath
                    ];
    cell.titleLbl.text = @"yo";

    // Switch colors to see view swipes... DELETE
    CGFloat hue = (CGFloat)indexPath.row / 9;
    cell.backgroundColor = [UIColor
                        colorWithHue:hue saturation:1.0f brightness:0.5f alpha:1.0f
                        ];
    cell.titleLbl.textColor = [UIColor whiteColor];
    cell.titleLbl.tintColor = [UIColor whiteColor];

    return cell;
}


- (CGSize)collectionView:(UICollectionView *)collectionView
              layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return collectionView.bounds.size;
}

this is a screenshot of my storyboard and connections -

enter image description here

This compiles ok but all i get is an empty full screen of color - which does scroll - but theres no evidence of anything else in my custom cell!? Where am I going wrong.

Dancer
  • 15,237
  • 35
  • 119
  • 194
  • A few things to check: 1) have you put a breakpoint in `cellForRowAtIndexPath` to verify you're getting a cell and that the connections are as expected? 2) Is the cell defined in the same storyboard as the collection view (drag a collection view cell to the collection view in interface builder and go from there) 3) have you verified that the size of the collection view is as expected? – David Berry Apr 22 '14 at 15:41
  • Thanks david - 1 - It does contain a version of my custom cell - 2 - yes and 3 - collection size is 9 - very frustrating! Thanks for your help though. – Dancer Apr 22 '14 at 16:54

1 Answers1

1

It looks like you have a collection view with your custom cell in the storyboard. If that is indeed the case, then you shouldn't be calling loadView, where you create another collection view -- this is a different collection view from the one you created in the storyboard, and won't have your cell in it. Try commenting out that whole method (you can put any of those statements that you need in viewDidLoad instead -- also make sure you have an IBOutlet to the collection view unless you're using a UICollectionViewController which already has that).

rdelmar
  • 102,832
  • 11
  • 203
  • 218