How to use collectionView method of CollectionViewController class

Best Swift-snapshot-testing code snippet using CollectionViewController.collectionView

PrayerDetailsViewController.swift

Source:PrayerDetailsViewController.swift Github

copy

Full Screen

...11    12    var currentIndexPath: IndexPath?13    var data: [Prayer] = []14    15    let collectionViewController: UICollectionViewController = {16        let collectionViewController = UICollectionViewController(collectionViewLayout: UICollectionViewFlowLayout())17        collectionViewController.collectionView.isPagingEnabled = true18        collectionViewController.collectionView.showsHorizontalScrollIndicator = false19        collectionViewController.collectionView.contentInset = UIEdgeInsets(top: 10, left: 0, bottom: 0, right: 0)20        collectionViewController.collectionView.alpha = 021        return collectionViewController22    }()23    24    let activityIndicatorView: UIActivityIndicatorView = {25        var activityIndicatorView: UIActivityIndicatorView26        27        if #available(iOS 13.0, *) {28            activityIndicatorView = UIActivityIndicatorView(style: .large)29        } else {30            // Fallback on earlier versions31            activityIndicatorView = UIActivityIndicatorView()32        }33        34        return activityIndicatorView35    }()36    let dismissLabel: Button = {37        let button = Button("Повернутися", fontSize: 14, fontColor: .lightGrayCustom)38        button.addTarget(self, action: #selector(dismissViewController), for: .touchUpInside)39        40        return button41    }()42    let dismissButton: UIButton = {43        let button = UIButton()44        let image = UIImage(named: "arrow-down")45        let imageTint = UIImage(named: "arrow-down")?.withRenderingMode(.alwaysTemplate)46        button.setImage(image, for: .normal)47        button.setImage(imageTint, for: .highlighted)48        button.addTarget(self, action: #selector(dismissViewController), for: .touchUpInside)49        50        return button51    }()52    53    override func viewDidLoad() {54        super.viewDidLoad()55        self.collectionViewController.collectionView.delegate = self56        self.collectionViewController.collectionView.dataSource = self57        // Do any additional setup after loading the view.58        self.collectionViewController.collectionView.register(PrayerDetailsCollectionViewCell.self, forCellWithReuseIdentifier: PrayerDetailsCollectionViewCell.reuseIdentifier)59        60        if let layout = self.collectionViewController.collectionView.collectionViewLayout as? UICollectionViewFlowLayout {61            layout.scrollDirection = .horizontal62            layout.minimumLineSpacing = 063            layout.minimumInteritemSpacing = 064        }65        66        if #available(iOS 13.0, *) {67            self.view.addSubviews([self.collectionViewController.collectionView, self.dismissButton, self.activityIndicatorView])68        } else {69            self.view.addSubviews([self.collectionViewController.collectionView, self.dismissLabel, self.dismissButton, self.activityIndicatorView])70        }71        72        self.activityIndicatorView.startAnimating()        73    }74    75    override func viewWillLayoutSubviews() {76        super.viewWillLayoutSubviews()77        setupLayout()78    }79    80    override func viewDidAppear(_ animated: Bool) {81        super.viewDidAppear(animated)82        // Do any additional setup before appearing the view.83        if let indexPath = self.currentIndexPath {84            if let rect = self.collectionViewController.collectionView.layoutAttributesForItem(at: indexPath)?.frame {85                DispatchQueue.main.async {86                    self.collectionViewController.collectionView.scrollRectToVisible(rect, animated: false)87                }88            }89            self.activityIndicatorView.stopAnimating()90            UIView.animate(withDuration: 0.5) {91                self.collectionViewController.collectionView.alpha = 192            }93            self.currentIndexPath = nil94        }95    }96    97    private func setupLayout() {98        self.view.backgroundColor = .white99        if #available(iOS 13.0, *) {100            self.dismissButton.anchor(top: self.view.topAnchor, padding: UIEdgeInsets(top: 8, left: 0, bottom: 0, right: 0), size: CGSize(width: 0, height: 16))101        } else {102            // Fallback on earlier versions103            self.dismissButton.anchor(top: self.view.topAnchor, padding: UIEdgeInsets(top: 48, left: 0, bottom: 0, right: 0), size: CGSize(width: 0, height: 16))104            self.dismissLabel.anchor(leading: self.view.leadingAnchor, bottom: self.dismissButton.topAnchor, trailing: self.view.trailingAnchor, padding: UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0))105        }106        self.dismissButton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true107        self.activityIndicatorView.anchor(centerY: self.view.centerYAnchor, centerX: self.view.centerXAnchor)108        self.collectionViewController.collectionView.backgroundColor = .white109//        self.collectionViewController.collectionView.anchor(top: self.dismissButton.bottomAnchor, leading: self.view.leadingAnchor, bottom: self.view.bottomAnchor, trailing: self.view.trailingAnchor, padding: .zero)110        self.collectionViewController.collectionView.pin.below(of: self.dismissButton).marginTop(0).horizontally().bottom()111    }112    113}114extension PrayerDetailsCollectionViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {115    116    // MARK: UICollectionViewDataSource117    118    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {119        let size = CGSize(width: collectionViewController.collectionView.bounds.size.width, height: collectionViewController.collectionView.bounds.size.height)120        return size121    }122    123    func numberOfSections(in collectionView: UICollectionView) -> Int {124        // #warning Incomplete implementation, return the number of sections125        return 1126    }127    128    129    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {130        // #warning Incomplete implementation, return the number of items131        return self.data.count132    }133    134    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {135        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PrayerDetailsCollectionViewCell.reuseIdentifier, for: indexPath) as? PrayerDetailsCollectionViewCell else { return UICollectionViewCell() }136        137        // Configure the cell138        let data = self.data[indexPath.item]139        cell.configureWithData(data: data)140        cell.layoutIfNeeded()141        return cell142    }143    144    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {145        print(indexPath)146    }147    148    149    @objc func dismissViewController(_ sender: UIButton!) {150        self.dismiss(animated: true, completion: nil)151    }152    153}...

Full Screen

Full Screen

CollectionViewController.swift

Source:CollectionViewController.swift Github

copy

Full Screen

...30import UIKit31public protocol CollectionViewDelegate: UICollectionViewDelegate {}32public protocol CollectionViewDataSource: UICollectionViewDataSource {33  /**34   Retrieves the data source items for the collectionView.35   - Returns: An Array of DataSourceItem objects.36   */37  var dataSourceItems: [DataSourceItem] { get }38}39extension UIViewController {40  /**41   A convenience property that provides access to the CollectionViewController.42   This is the recommended method of accessing the CollectionViewController43   through child UIViewControllers.44   */45  public var collectionViewController: CollectionViewController? {46    return traverseViewControllerHierarchyForClassType()47  }48}49open class CollectionViewController: ViewController {50  /// A reference to a Reminder.51  open let collectionView = CollectionView()52  53  open var dataSourceItems = [DataSourceItem]()54  55  open override func prepare() {56    super.prepare()57    prepareCollectionView()58  }59  60  open override func layoutSubviews() {61    super.layoutSubviews()62    layoutCollectionView()63  }64}65extension CollectionViewController {66  /// Prepares the collectionView.67  fileprivate func prepareCollectionView() {68    collectionView.delegate = self69    collectionView.dataSource = self70    view.layout(collectionView).edges()71  }72}73extension CollectionViewController {74  /// Sets the frame for the collectionView.75  fileprivate func layoutCollectionView() {76    collectionView.frame = view.bounds77  }78}79extension CollectionViewController: CollectionViewDelegate {}80extension CollectionViewController: CollectionViewDataSource {81  @objc82  open func numberOfSections(in collectionView: UICollectionView) -> Int {83    return 184  }85  86  @objc87  open func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {88    return dataSourceItems.count89  }90  91  @objc92  open func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {93    return collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath)94  }95}...

Full Screen

Full Screen

WatchlistViewController.swift

Source:WatchlistViewController.swift Github

copy

Full Screen

...3class WatchlistViewController: MainViewController {4    override func viewDidLoad() {5        super.viewDidLoad()6        7        collectionViewController.paginated = false8    }9    10    override func viewWillAppear(_ animated: Bool) {11        load(page: -1) // Refresh watchlsit12        super.viewWillAppear(animated)13    }14    15    override func load(page: Int) {16        let group = DispatchGroup()17        18        group.enter()19            self.collectionViewController.dataSources = [WatchlistManager<Movie>.movie.getWatchlist { [unowned self] (updated) in20                self.collectionViewController.dataSources[0] = updated.sorted(by: {$0.title < $1.title})21                self.collectionViewController.collectionView?.reloadData()22                self.collectionViewController.collectionView?.collectionViewLayout.invalidateLayout()23            }.sorted(by: {$0.title < $1.title})]24        self.collectionViewController.dataSources.append(WatchlistManager<Show>.show.getWatchlist { [unowned self] (updated) in25                self.collectionViewController.dataSources[1] = updated.sorted(by: {$0.title < $1.title})26                self.collectionViewController.collectionView?.reloadData()27    self.collectionViewController.collectionView?.collectionViewLayout.invalidateLayout()28            }.sorted(by: {$0.title < $1.title}))29        30            self.collectionViewController.collectionView?.reloadData()31        self.collectionViewController.collectionView?.collectionViewLayout.invalidateLayout()32        33    }34    35    override func collectionView(_ collectionView: UICollectionView, titleForHeaderInSection section: Int) -> String? {36        if section == 0 {37            return "Movies".localized38        } else if section == 1 {39            return "Shows".localized40        }41        return nil42    }43    44    override func collectionView(_ collectionView: UICollectionView, insetForSectionAt section: Int) -> UIEdgeInsets? {45        let isTv = UIDevice.current.userInterfaceIdiom == .tv46        47        return isTv ? UIEdgeInsets(top: 60, left: 90, bottom: 0, right: 90) : UIEdgeInsets(top: 5, left: 15, bottom: 15, right: 15)48    }49    50    override func collectionView(isEmptyForUnknownReason collectionView: UICollectionView) {51        if let background: ErrorBackgroundView = .fromNib() {52            background.setUpView(title: "Watchlist Empty".localized, description: "Try adding movies or shows to your watchlist.".localized)53            collectionView.backgroundView = background54        }55    }56}...

Full Screen

Full Screen

collectionView

Using AI Code Generation

copy

Full Screen

1class CollectionViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {2    override func viewDidLoad() {3        super.viewDidLoad()4        let layout = UICollectionViewFlowLayout()5        collectionView = UICollectionView(frame: view.frame, collectionViewLayout: layout)6        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")7        view.addSubview(collectionView)8    }9    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {10    }11    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {12        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)13    }14    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {15        return CGSize(width: view.frame.width, height: 50)16    }17}

Full Screen

Full Screen

collectionView

Using AI Code Generation

copy

Full Screen

1import UIKit2class CollectionViewController: UICollectionViewController {3    override func viewDidLoad() {4        super.viewDidLoad()5        collectionView?.register(CollectionViewCell.self, forCellWithReuseIdentifier: "cellId")6    }7    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {8    }9    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {10        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! CollectionViewCell11    }12}13import UIKit14class CollectionViewController: UICollectionViewController {15    override func viewDidLoad() {16        super.viewDidLoad()17        collectionView?.register(CollectionViewCell.self, forCellWithReuseIdentifier: "cellId")18    }19    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {20    }21    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {22        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! CollectionViewCell23    }24}25import UIKit26class CollectionViewController: UICollectionViewController {27    override func viewDidLoad() {28        super.viewDidLoad()29        collectionView?.register(CollectionViewCell.self, forCellWithReuseIdentifier: "cellId")30    }31    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {32    }33    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {34        let cell = collectionView.dequeueReusableCell(with

Full Screen

Full Screen

collectionView

Using AI Code Generation

copy

Full Screen

1import UIKit2class CollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {3    var images = [UIImage]()4    override func viewDidLoad() {5        super.viewDidLoad()6        self.collectionViewLayout = UICollectionViewFlowLayout()7        self.collectionViewLayout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)8        self.collectionViewLayout.itemSize = CGSize(width: 100, height: 100)9        self.collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: self.collectionViewLayout)10        self.collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")11        self.view.addSubview(self.collectionView)12        self.images.append(UIImage(named: "1")!)13        self.images.append(UIImage(named: "2")!)14        self.images.append(UIImage(named: "3")!)15        self.images.append(UIImage(named: "4")!)16        self.images.append(UIImage(named: "5")!)17        self.images.append(UIImage(named: "6")!)18        self.images.append(UIImage(named: "7")!)19        self.images.append(UIImage(named: "8")!)20        self.images.append(UIImage(named: "9")!)21        self.images.append(UIImage(named: "10")!)22        self.images.append(UIImage(named: "11")!)23        self.images.append(UIImage(named: "12")!)24        self.images.append(UIImage(named: "13")!)25        self.images.append(UIImage(named: "14")!)26        self.images.append(UIImage(named: "15")!)27        self.images.append(UIImage(named: "16")!)28        self.images.append(UIImage(named: "17")!)29        self.images.append(UIImage(named: "18")!)30        self.images.append(UIImage(named: "19")!)31        self.images.append(UIImage(named: "20")!)32        self.images.append(UIImage(named: "21")!)33        self.images.append(UIImage(named: "22")!)34        self.images.append(UIImage

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Swift-snapshot-testing automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in CollectionViewController

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful