Swift*UICollectionView uses storyBoard to complete automatic layout settings with a simple selection function

Swift*UICollectionView uses storyBoard to complete automatic layout settings with a simple selection function
text automatic layout, with multiple choice

One, storyBoard to achieve UICollectionView, and add UICollectionViewCell.

UICollectionView: set the boundary position

TextLabelCell (UICollectionViewCell):
backView- select the background frame, and the same
contentLbl- boundary cell content display, set the display center, do not set the size, do not set up and down position.

Swift*UICollectionView uses storyBoard to complete automatic layout settings with a simple selection function
collectionView
Swift*UICollectionView uses storyBoard to complete automatic layout settings with a simple selection function
cell-label

Association agent, implementation agent method.

Two. UICollectionView sets the adaptive size

Implementation of proxy method do not need to specify the size of cell, only three
numberOfSections
numberOfItemsInSection
cellForItemAt agent indexPath
can achieve normal function.

Cell size / / set to determine the adaptive mode, size cannot be empty, you can set a (collectionView.collectionViewLayout as! UICollectionViewFlowLayout.EstimatedItemSize CGSize (width:) = 20, height: 20)

Three, modify the UICollectionViewCell property

In TextLabelCell, override the preferredLayoutAttributesFitting method, where you can customize the set cell size

Override func preferredLayoutAttributesFitting (_ layoutAttributes: UICollectionViewLayoutAttributes -> UICollectionViewLayoutAttributes) {let attributes = super.preferredLayoutAttributesFitting (layoutAttributes) attributes.frame = CGRect (X: 0, y: 0, width:NSString (string: contentLbl.text!).Size (attributes: [NSFontAttributeName:contentLbl.font]).Width+40, height: 40) / / cellSize calculations, the current requirements of 40 fixed height, width, and now according to the string obtained the required width of + 42, contentLabel relative to cell 20 around each of the space flexible return / / attributes} according to specific needs

If you set the UICOllectionViewFlowLayout property only and do not override the cell method, there will be problems during reuse, causing the requested size to be incorrect and unable to determine the size of the cell itself.

Four, the final result

Complete the main two steps above to implement the adaptive size function, and, as a small requirement, do a multiple choice processing, here is the complete code:

Swift*UICollectionView uses storyBoard to complete automatic layout settings with a simple selection function
final results

ViewController.swift

Class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {var selectedIndexArray:Array = Array< (VaR); IndexPath> dataArray:Array = Array< String> (@IBOutlet) var collectionView: UICollectionView override func viewDidLoad (!) {super.viewDidLoad (any) Do additional setup after loading / the view, typically from a nib. is set to determine the adaptive cell / size (collectionView.collectionViewLayout as! UICollectionViewFlowLayout.EstimatedItemSize CGSize (width:) = 20, height: 20 (collectionView.collectionViewLayout) / / set lateral spacing as! UICollectionViewFlowLayout).MinimumInteritemSpacing = 5 Set / longitudinal spacing line spacing (collectionView.collectionViewLayout - as! UICollectionViewFlowLayout).MinimumLineSpacing = 20 dataArray.append ("the first second third") for _ in 0..< 100 index (arc4random) {let =%5; dataArray.append ("/ (index*index*index*500 + 8)}}") / / MARK: UICollectionView DataSource func numberOfSections (in collectionView: UICollectionView -> Int) {return 1;} func (collectionView collectionView: UICollectionView numberOfItemsInSection section: Int _, -> Int) {return dataArray.count;} func (collectionView collectionView: UICollectionView cellForItemAt indexPath: In _. DexPath -> UICollectionViewCell) {let cell = collectionView.dequeueReusableCell (withReuseIdentifier: TextLabelCell, for: indexPath) as cell.contentLbl.text cell.cellStatusWithSelected (TextLabelCell! = dataArray[indexPath.row] (selected: (selectedIndexArray.index (of: indexPath)! = Nil))) return cell func collectionView (collectionView: _} UICollectionView didSelectItemAt, indexPath: IndexPath) {let cell = collectionView.cellForItem (at: indexPath) as! TextLabelCell if let I = selectedIndexArray.index (of: indexPath) {print ("selected:/ (I)") selectedIndexArray.remove (at: I) cell.cellStatusWithSelected (selected: false E)} LSE {selectedIndexArray.append (indexPath) cell.cellStatusWithSelected (selected: true)}} / / @IBAction func saveButtonClicked (sender: _ save Any for index in selectedIndexArray) {{print}} (dataArray[index.row]) override func (didReceiveMemoryWarning) {super.didReceiveMemoryWarning (of) any resources that can / Dispose be recreated.}}

TextLabelCell.swift

Class TextLabelCell: UICollectionViewCell @IBOutlet var contentLbl: UILabel {@IBOutlet var backView:! UIView! Override func (awakeFromNib) {backView.layer.borderWidth = 1; backView.layer.borderColor = UIColor.lightGray.cgColor;} func {if selected cellStatusWithSelected (selected:Bool) {backView.layer.borderColor = UIColor.red.cgColor; contentLbl.textColor = UIColor.red;} else {backView.layer.borderColor = UIColor.lightGray.cgColor; contentLbl.textColor = UIColor.lightGray;}} override func preferredLayoutAttributesFitting (_ layoutAttributes: UICollectionViewLayoutAttributes -> UICollectionV) IewLayoutAttributes {let attributes = super.preferredLayoutAttributesFitting (layoutAttributes) attributes.frame = CGRect (x: 0, y: 0, width:NSString (string: contentLbl.text!).Size (attributes: [NSFontAttributeName:contentLbl.font]).Width+40, height: 40) / / cellSize calculations, the current requirements of 40 fixed height, width, and now calculate the required width according to the string +42, contentLabel relative to cell around each 20 of the space flexible return / / attributes}} according to specific needs