I have a question about xcode7.x Swift (about collection view)

Asked 2 years ago, Updated 2 years ago, 32 views

Hello, I am a student who is developing an application with Swift!

Today, I am posting a question because I have a question during the data transfer between classes.

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int

I have 3 questions

The order only worked the first time Is there a way to call this function several times? For example, I want to give a performance that is called every time I select in a different table view and changes items.

Is this a function that gives an initial value?

What is the nature of these override views that you provide basically every time you create a class for each view and change the subclass? Do you have any reference materials?

ios swift

2022-09-22 21:40

1 Answers

1 - Change the data used in UICollectionView and call the reloadData() method. (The same goes for UITableView.)

collection.reloadData()

2 - Method called when data is initialized or reloaded in UICollectionView.

3 - Methods that must be included in the Delegate or DataSource. In UICollectionView, this is the minimum method required for initialization or reload. The rest of the annotated methods are like options that you use if you need to.

 //MARK: Collection View Data Source & Dellegate
    //Number of Collection View Sections
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1;
    }

    //Number of cell items
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }

    //Cell Item Definition
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! UICollectionViewCell

        return cell

    } 

    //Call when cell item is selected
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    }

    //cell size
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSizeMake(100, 100)
    }

    //height interval
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
        return 0;
    }
    //width interval
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
        return 0;
    }

If you read the link below step by step for reference, you will be able to learn some of the UICollectionview.

Collection View #1. iOS App's Simplified UI implementation Powerfully!

Collection View #2.10 minutes completing GridUI

Collection View (UICollectionView) #3. More about cells in depth Let's dig in.

Collection View (UICollectionView) #4. Edit Menu Add!

Collection View #5. Layout, And Flow Layout

If you have any questions, please leave a comment! And if I am mistaken or wrong, please feel free to request editing :)


2022-09-22 21:40

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.