简体   繁体   中英

2 UICollectionViews in 1 View Controller working with swift and xcode

So i am working in xcode with swift on my storyboard I have only 1 view controller on that view controller i have 2 separate collection view cells, top 1 will show certain images and the bottom shows a different array of images

my problem is that when i connect dataSource and delegate to viewcontroller for both of the views the program crashes and i get the following error:

[OF.ViewController collectionView:numberOfItemsInSection:]: unrecognized selector sent to instance 0x7b662cc0 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[OF.ViewController collectionView:numberOfItemsInSection:]: unrecognized selector sent to instance 0x7b662cc0'

and if i disconnect both dataSource and ViewController then the app runs but nothing shows up in the collection cells

the functions:

func topsCollectionView(topCollectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return topImages.count
}

func bottomsCollectionView(bottomCollectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return bottomImages.count
}

Your app is crashing because OF.ViewController doesn't implement collectionView:numberOfItemsInSection: .

Instead you have: topsCollectionView:numberOfItemsInSection: and bottomsCollectionView:numberOfItemsInSection: . To fix this problem, implement the dataSource method collectionView:numberOfItemsInSection: . Then, you can differentiate between the top and bottom UICollectionViews by way of comparison.

Ex.

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSInteger numberOfItems = 0;

    if (collectionView == self.topCollectionView)
        numberOfItems = [self.topImages count];
    else if (collectionView == self.bottomCollectionView)
        numberOfItems = [self.bottomImages count];

    /* OR, if you prefer one line*/
    numberOfItems = (collectionView == self.topCollectionView ? [self.topImages count] : [self.bottomImages count]);

    return numberOfItems;
}

PS I apologize, I don't know Swift.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM