微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决多个 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 举报,一经查实,本站将立刻删除。