简体   繁体   中英

Getting the Count of each array inside a 2D Array in Swift

So I have a 2D array of colors as described here:

var array: [[UIColor]] = [[UIColor.black, UIColor.blue, UIColor.green], [UIColor.gray, UIColor.darkGray, UIColor.yellow, UIColor.red]]

and what I'm try to get is the count of both arrays inside the main array.

The purpose.... I'm using a collection view inside my table view cell to display the colors. I've written the code to display only 5 cells (I want a limit of 5 cells) but I'd like the cells to dynamically change the size to conform to the device width.

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        return CGSize(width: view.frame.width / 5, height: 100)
    }

This is the image of the simulator to portray what I'm trying to get across:

在此处输入图像描述

extension MyTableViewCell {

    func setCollectionViewDataSourceDelegate<D: UICollectionViewDataSource & UICollectionViewDelegate>(_ dataSourceDelegate: D, forRow row: Int) {

        collectionView.delegate = dataSourceDelegate
        collectionView.dataSource = dataSourceDelegate
        collectionView.tag = row
        collectionView.setContentOffset(collectionView.contentOffset, animated:false) // Stops collection view if it was scrolling.
        collectionView.reloadData()
    }
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return array[collectionView.tag].count
    }

You could use the map function for this.

Disclaimer: typed in browser, not tested

let countedArray: [Int] = array.map { $0.count }

You need to get the indexPath 's section property and use it as an index to subscript your array and get the number of elements of that section. You will need to coerce it to CGFloat to use it as a divisor:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
   .init(width: view.frame.width / CGFloat(array[indexPath.section].count), height: 100)
}

 override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { array[section].count  }

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

In your viewDidLoad:

let layout = UICollectionViewFlowLayout()
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
collectionView?.collectionViewLayout = layout

Don't forget to invalidate your layout on device rotation:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
    (collectionView.collectionViewLayout as? UICollectionViewFlowLayout)?.invalidateLayout()
}

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