多个 uilabel 选择并快速存储在数组中

问题描述

如何基于布尔值选择和取消选择多个文本并存储在数组中?现在我正在获取 api 并在 uicollectionview 中水平显示在 uilabel 中。例如,我从 api 获取三个文本(Text1、Text2、Text3)如何选择所有三个文本并存储在 int 数组中?以下是我到目前为止所做的快速代码

class EatTypeCell : UICollectionViewCell{
    @IBOutlet weak var lblTitle: UILabel!
    override var isSelected: Bool{
        didSet{
            if isSelected{ // Here if i get three texts i am selecting only one
                self.lblTitle.applyStyle(labelFont: UIFont.applySemiBold(fontSize: 10),textColor: UIColor.ColorWhite)
                self.contentView.applyViewStyle(isRound: true,borderColor: UIColor.ColorGray,borderWidth: 0,backGroundColor: UIColor.ColorPink)
            }
            else{
                self.lblTitle.applyStyle(labelFont: UIFont.applySemiBold(fontSize: 10),textColor: UIColor.ColorGray)
                self.contentView.applyViewStyle(isRound: true,borderWidth: 1,backGroundColor: UIColor.ColorWhite)
            }
        }
    }
    override func awakeFromNib() {
        self.contentView.applyViewStyle(isRound: true,backGroundColor: UIColor.ColorWhite)
        self.lblTitle.applyStyle(labelFont: UIFont.applySemiBold(fontSize: 10),textColor: UIColor.ColorGray)
    }
}
class OfferVC: UIViewController {
    @IBOutlet weak var btnFind: ThemeButton!
    @IBOutlet weak var colEat: UICollectionView!
    var arrayMeal : [Meal] = []
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }   
    deinit {
        
    }
    func setUpView() {
        GFunctions.shared.APICallMenuMeal { (complete,arrayItem) in //API Call
            if complete{
                self.arrayMeal = arrayItem
                self.colEat.reloadData()
            }
        }
        self.setViews()
        self.colEat.delegate = self
        self.colEat.dataSource = self   
    }
    func setViews(){  
        self.btnFind.backgroundColor = .ColorWhite
        self.btnFind.setTitleColor(UIColor.ColorPink,for: UIControl.State())
    }
    @IBAction func btnFindTapped(_ sender: ThemeButton) {
            filterMenId = ""
            if let indexPath = self.colEat.indexPathsForSelectedItems?.first{
                filterMenId = self.arrayMeal[indexPath.row].id.description 
                print(filterMenId) // Here i am getting only one selected id
            }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        setUpView()
    }
}
extension OfferVC : UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
        if self.colEat == collectionView{
            return self.arrayMeal.count
        }
    }
    func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if self.colEat == collectionView{
        let cell = self.colEat.dequeueReusableCell(withReuseIdentifier: "EatTypeCell",for: indexPath) as! EatTypeCell
        cell.lblTitle.text = self.arrayMeal[indexPath.row].name
        return cell
        } 
    }
    func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeforItemAt indexPath: IndexPath) -> CGSize {
        let height = collectionView.frame.height/2 - 10
        var width : CGFloat = 0
        if self.colEat == collectionView {
        width = self.arrayMeal[indexPath.row].name.sizeOfString(font: UIFont.applySemiBold(fontSize: 10)).width + 30
        } 
        return CGSize(width: width,height: height)
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)