如何从数组中获取 UICollectionView 的所有正确值
How to get all correct values of UICollectionView from Array
我正在获取 api 调用并获取数据文本(文本 1、文本 2、文本 3),每个文本都有自己的 ID(文本 1 = 32、文本 2 = 33、文本 3 = 34 ).因此,每当我 select 任何文本(Text1 或 Text2 或 Text3)存储在数组和打印中时,我都可以看到 id 值。但现在的问题是,如果我 select 多个文本,那么在打印结果中它不会显示正确的 ID。意味着起初我 select text1 并且在打印中它给了我 32 现在我 select 剩余的文本所以我希望得到 ["32","33","34"] 但它给了我 [" 32", "34"] 如果我删除 select 任何文本,那么它仍然像这样打印两到三次 ["32", "34", "32", "32"]
class EatTypeCell : UICollectionViewCell{
@IBOutlet weak var lblTitle: UILabel!
override var isSelected: Bool{
didSet{
if isSelected{
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, borderColor: UIColor.ColorGray, borderWidth: 1, backGroundColor: UIColor.ColorWhite)
}
}
}
override func awakeFromNib() {
self.contentView.applyViewStyle(isRound: true, borderColor: UIColor.ColorGray, borderWidth: 1, backGroundColor: UIColor.ColorWhite)
self.lblTitle.applyStyle(labelFont: UIFont.applySemiBold(fontSize: 10), textColor: UIColor.ColorGray)
}
}
class OfferVC: UIViewController {
@IBOutlet weak var colEat: UICollectionView!
@IBOutlet weak var btnFind: ThemeButton!
var arrayMeal : [Meal] = []
var num : [String] = []
func setUpView() {
GFunctions.shared.APICallMenuMeal { (complete, arrayItem) in //API Call
if complete{
self.arrayMeal = arrayItem
self.colEat.reloadData()
}
}
self.colEat.delegate = self
self.colEat.dataSource = self
self.colEat.allowsMultipleSelection = true
@IBAction func btnFindTapped(_ sender: ThemeButton) {
filterMenId = ""
if let indexPath = self.colEat.indexPathsForSelectedItems?.first{
filterMenId = self.arrayMeal[indexPath.row].id.description
num.append(filterMenId)
print(num)// Here printing to see the stored values in array
}
override func viewDidLoad() {
super.viewDidLoad()
setUpView()
}
}
不清楚您的按钮何时被点击以及集合当时处于何种状态,因此我建议打印 indexPathsForSelectedItems
以检查点击按钮时它实际拥有的项目.
无论如何,如果您只想一次获取所有选定项目的值,那么您可以简单地这样做:
@IBAction func btnFindTapped(_ sender: ThemeButton) {
num = self.colEat.indexPathsForSelectedItems?.map {
self.arrayMeal[[=10=].item].id.description
}
}
如果您想逐步更新数组,那么我建议分别在 collectionView(_:didSelectItemAt:) and collectionView(_:didDeselectItemAt:) 中进行处理:
extension OfferVC: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
num.append(self.arrayMeal[indexPath.item].id.description)
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
num.removeAll { [=11=] == self.arrayMeal[indexPath.item].id.description }
}
}
我正在获取 api 调用并获取数据文本(文本 1、文本 2、文本 3),每个文本都有自己的 ID(文本 1 = 32、文本 2 = 33、文本 3 = 34 ).因此,每当我 select 任何文本(Text1 或 Text2 或 Text3)存储在数组和打印中时,我都可以看到 id 值。但现在的问题是,如果我 select 多个文本,那么在打印结果中它不会显示正确的 ID。意味着起初我 select text1 并且在打印中它给了我 32 现在我 select 剩余的文本所以我希望得到 ["32","33","34"] 但它给了我 [" 32", "34"] 如果我删除 select 任何文本,那么它仍然像这样打印两到三次 ["32", "34", "32", "32"]
class EatTypeCell : UICollectionViewCell{
@IBOutlet weak var lblTitle: UILabel!
override var isSelected: Bool{
didSet{
if isSelected{
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, borderColor: UIColor.ColorGray, borderWidth: 1, backGroundColor: UIColor.ColorWhite)
}
}
}
override func awakeFromNib() {
self.contentView.applyViewStyle(isRound: true, borderColor: UIColor.ColorGray, borderWidth: 1, backGroundColor: UIColor.ColorWhite)
self.lblTitle.applyStyle(labelFont: UIFont.applySemiBold(fontSize: 10), textColor: UIColor.ColorGray)
}
}
class OfferVC: UIViewController {
@IBOutlet weak var colEat: UICollectionView!
@IBOutlet weak var btnFind: ThemeButton!
var arrayMeal : [Meal] = []
var num : [String] = []
func setUpView() {
GFunctions.shared.APICallMenuMeal { (complete, arrayItem) in //API Call
if complete{
self.arrayMeal = arrayItem
self.colEat.reloadData()
}
}
self.colEat.delegate = self
self.colEat.dataSource = self
self.colEat.allowsMultipleSelection = true
@IBAction func btnFindTapped(_ sender: ThemeButton) {
filterMenId = ""
if let indexPath = self.colEat.indexPathsForSelectedItems?.first{
filterMenId = self.arrayMeal[indexPath.row].id.description
num.append(filterMenId)
print(num)// Here printing to see the stored values in array
}
override func viewDidLoad() {
super.viewDidLoad()
setUpView()
}
}
不清楚您的按钮何时被点击以及集合当时处于何种状态,因此我建议打印 indexPathsForSelectedItems
以检查点击按钮时它实际拥有的项目.
无论如何,如果您只想一次获取所有选定项目的值,那么您可以简单地这样做:
@IBAction func btnFindTapped(_ sender: ThemeButton) {
num = self.colEat.indexPathsForSelectedItems?.map {
self.arrayMeal[[=10=].item].id.description
}
}
如果您想逐步更新数组,那么我建议分别在 collectionView(_:didSelectItemAt:) and collectionView(_:didDeselectItemAt:) 中进行处理:
extension OfferVC: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
num.append(self.arrayMeal[indexPath.item].id.description)
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
num.removeAll { [=11=] == self.arrayMeal[indexPath.item].id.description }
}
}