Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Native Ads #1

Open
3rsin3rgul opened this issue Oct 11, 2018 · 3 comments
Open

Native Ads #1

3rsin3rgul opened this issue Oct 11, 2018 · 3 comments

Comments

@3rsin3rgul
Copy link

Hello,
I have a native ads and i want to return 1 column , other items will be return 2. But i couldnt do it.
Can you help me about this? My ads repeating itself in every 9 item.
Those are my codes
screen shot 2018-10-11 at 11 16 58
screen shot 2018-10-11 at 11 17 37

@regexident
Copy link
Owner

Assuming I understood your intentions I'd probably go with something like this:

import CollectionViewMultiColumnLayout

enum Section {
    case items([Item])
    case ads([Ad])

    var numberOfItems: Int {
        switch self {
            case items(let items): return items.count
            case ads(let ads): return ads.count
        }
    }
    
    var numberOfColumns: Int {
        return self.numberOfItems
    }
}

class CollectionViewController: UICollectionViewController {
    enum ReuseIdentifier {
        static let itemCell: String = "itemCell"
        static let adCell: String = "adCell"
    }

    static let numberOfItemColumns: Int = 2
    static let numberOfAdColumns: Int = 1

    let sections: [Section] = 

    override func viewDidLoad() {
        super.viewDidLoad()
        let layout = CollectionViewMultiColumnLayout()
        collectionView.setCollectionViewLayout(layout, animated: false)
    }

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return self.sections.count
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.sections[section].numberOfItems
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let section = self.sections[indexPath.section]
        let cell: UICollectionViewCell
        switch section {
        case .items(let items):
            let item = items[indexPath.row]
            cell = collectionView.dequeueReusableCell(
                withReuseIdentifier: ReuseIdentifier.itemCell,
                for: indexPath
            )
            // prepare `cell` for `item`
        case .ads(let ads):
            let ad = ads[indexPath.row]
            cell = collectionView.dequeueReusableCell(
                withReuseIdentifier: ReuseIdentifier.adCell,
                for: indexPath
            )
            // prepare `cell` for `ad`
        }
        return cell
    }
}

extension CollectionViewController: CollectionViewMultiColumnLayoutDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfColumnsInSection section: Int) -> Int {
        return self.sections[section].numberOfItems
    }

    func collectionView(_ collectionView: UICollectionView, columnForItemAt indexPath: NSIndexPath) -> Int? {
        // Row-major order:
        switch self.sections[indexPath.section] {
        case .items(_):
            return indexPath.row % CollectionViewController.numberOfItemColumns
        case .ads(_):
            return indexPath.row % CollectionViewController.numberOfAdColumns
        }
    }
}

@3rsin3rgul
Copy link
Author

3rsin3rgul commented Oct 12, 2018 via email

@regexident
Copy link
Owner

In that case you'll basically have to either:

  • figure out a way to map from your items-only MoPub data source to one with items interleaved with ad sections, ad-hoc.
  • or replace the case ad([ads]) with case ad, acting as a place-holder, to make index-path math easier and load the ad dynamically.

I'm not experienced with MoPub and somewhat busy right now, sry. :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants