Home > Software engineering >  Sort data function in the CollectionView. Swift
Sort data function in the CollectionView. Swift

Time:04-13

I have a collectionView with some data. There are 8 cells with labels. How can I remove cell from collection if torque < 0 I tried to create the class of data for labels and the sort this data, but I found the way how to sort only data of one label Example: collection view Collection View

View Controller

import UIKit

 class ViewController: UIViewController, UICollectionViewDataSource,   UICollectionViewDelegate {


@IBOutlet weak var itemCollection: UICollectionView!
@IBOutlet weak var collectionView: UICollectionViewCell!
@IBOutlet weak var sortSwitch: UISwitch!

let  bodytype = ["Abarth", "Acura", "Alfa Romeo", "Aston Martin", "Audi", "Bentley", "BMW","Buick"]
var force = [2.0, 3.0, 5.0, 4.6, 8.0, 5.0, 6.0, 2.0]
var torque = [-6.0, 36.0, 57.0, -48.6, 8.0, 5.0, -6.0, 32.0]
var lastSelectedIndexPath:IndexPath?


@IBAction func add(_ sender: Any) {
    
    reload()
    
}

@IBAction func sort(_ sender: Any) {
    
   

    if sortSwitch.isOn {
        sortSwitch.thumbTintColor = .blue
    } else {
        sortSwitch.thumbTintColor = .green
    }
}



func reload() -> Int {

    DispatchQueue.main.async {
        self.itemCollection.reloadData()
        self.lastSelectedIndexPath?.section = 1
    }
    
    let reloadInt = self.lastSelectedIndexPath?.row ?? 0
    print(reloadInt)
    return reloadInt
    
  }

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    bodytype.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
//cell
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "check", for: indexPath) as? checkCollectionViewCell
//icon image
    let smallConfig = UIImage.SymbolConfiguration(pointSize: 20, weight: .regular, scale: .medium)
    let stringforSymbol = ["sun.max","moon","circle","circle","circle","circle","sparkles","sparkles"]
    
    
// data
    let obj = UIImage(systemName: "\(stringforSymbol[indexPath.row])", withConfiguration: smallConfig)
    cell?.objectSymbol.setImage(obj, for: .normal)
    cell?.bodyname.text = (bodytype[indexPath.row])
    cell?.force.text = "\(force[indexPath.row])"
    cell?.torque.text = "\(torque[indexPath.row])"
    
//design
    cell?.layer.backgroundColor = UIColor.blue.cgColor
    cell?.layer.cornerRadius = 10
    cell?.isSelected = (lastSelectedIndexPath == indexPath)
    cell?.checkButtonSymbol.layer.cornerRadius = (cell?.checkButtonSymbol.bounds.size.width)! / 2    
 return cell!
}


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}
 }

CollectionViewCell

import UIKit

 class checkCollectionViewCell: UICollectionViewCell {

let color = #colorLiteral(red: 0.08444251865, green: 0.09528752416, blue: 0.09916398674, alpha: 1)

@IBOutlet weak var bodyname: UILabel!



@IBOutlet weak var force: UILabel!
@IBOutlet weak var torque: UILabel!

@IBOutlet weak var objectSymbol: UIButton!
@IBOutlet weak var checkButtonSymbol: UIButton!
//Image for check button
let notChecked = UIImage(systemName: "circle", withConfiguration: UIImage.SymbolConfiguration(pointSize: 20, weight: .regular, scale: .medium) )
let checked = UIImage(systemName: "checkmark.circle.fill", withConfiguration: UIImage.SymbolConfiguration(pointSize: 20, weight: .regular, scale: .medium) )

//Func check uncheck

override var isSelected: Bool{
    
    didSet (newValue){
        
        if(self.isSelected) &&  newValue == true {
            checkButtonSymbol.setImage(checked, for: .normal)
            checkButtonSymbol.backgroundColor = #colorLiteral(red: 0, green: 0.5904805064, blue: 1, alpha: 1)
            checkButtonSymbol.tintColor = #colorLiteral(red: 0.08444251865, green: 0.09528752416, blue: 0.09916398674, alpha: 1)
            
            print("new Value")
        
            
            
            
        } else {
            
            checkButtonSymbol.setImage(notChecked, for: .normal)
            checkButtonSymbol.backgroundColor = #colorLiteral(red: 0.08444251865, green: 0.09528752416, blue: 0.09916398674, alpha: 1)
            checkButtonSymbol.tintColor = #colorLiteral(red: 0, green: 0.3480696082, blue: 0.6240261197, alpha: 1)
        }
    }
}
 }

Model

struct Model {
var force: Double
var torque: Double
var position: Int


 }

  let arrayToSort = [Model(force: 202.0, torque: 450.0, position: 1), Model(force: 100.0, torque: 300.0, position: 2), Model(force: 500.0, torque: -200.0, position: 3), Model(force: 700.0, torque: -300.0, position: 4), Model(force: 700.0, torque: 800.0, position: 5)]
 let sortedArray = arrayToSort.sorted { $0.torque > $1.torque  }

 let indexPosition = sortedArray.map {$0.position }

 print(indexPosition)

CodePudding user response:

If you need to display cells in the table for which the statement that torque < 0 is true

Then you need to use one array for collection, which will contain structures, instead of three different arrays

For example:

struct Car {
    var force: Double
    var torque: Double
    var bodyType: String
    var stringForSymbol: String
}

let collectionDataArray = [
    Car(force: 25, torque: -1.0, bodyType: "Ferrari", stringForSymbol: "moon"),
    Car(force: 21, torque: 7.0, bodyType: "Acura", stringForSymbol: "circle")
]

If you initially have data on cars in the controller, then in the viewDidLoad method you need to remove all unnecessary elements from the array

override func viewDidLoad() {
    super.viewDidLoad()
    collectionDataArray.removeAll { $0.torque < 0 }

    // Methods for setup subviews (for example setup collectionView)
    // or just yourCollection.reloadData()
    setupCollection()
}

In this method, specify the array of your models

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    collectionDataArray.count
}

For cell configuration, you can use this implementation

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "check", for: indexPath) as? checkCollectionViewCell

    guard let cell = cell else { return UICollectionViewCell() }

    let smallConfig = UIImage.SymbolConfiguration(pointSize: 20, weight: .regular, scale: .medium)
    
    let obj = UIImage(systemName: "\(collectionDataArray[indexPath.row].stringforSymbol)", withConfiguration: smallConfig)
    cell.objectSymbol.setImage(obj, for: .normal)
    cell.bodyname.text = (collectionDataArray[indexPath.row].bodyType)
    cell.force.text = "\(collectionDataArray[indexPath.row].force)"
    cell.torque.text = "\(collectionDataArray[indexPath.row].torque)"
    
    cell.layer.backgroundColor = UIColor.blue.cgColor
    cell.layer.cornerRadius = 10
    cell.isSelected = (lastSelectedIndexPath == indexPath)
    cell.checkButtonSymbol.layer.cornerRadius = (cell.checkButtonSymbol.bounds.size.width)! / 2  
  
 return cell
}
  • Related