gpt4 book ai didi

ios - 如何将数组中的元素与indexPath.item进行比较?

转载 作者:行者123 更新时间:2023-11-29 05:50:08 24 4
gpt4 key购买 nike

当我使用 didSelectItemAt indexPath 时,项目已添加到数组中并且数组包含多个元素,但只有一个单元格具有“矩形填充”图片

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
indexSelected = indexPath.item

if trashButtonIsActive == true {
selectedItems.append(indexPath.item)
// I am adding indexPath.item to the array.Which need to compare with indexPath.item in cellForItemAt method.//
} else if trashButtonIsActive == false {
if selectedItems.count != 0 {
selectedItems.removeAll()
}

self.performSegue(withIdentifier: "cardView", sender: indexPath)
}

self.collectionView.reloadData()
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if trashButtonIsActive == true && selectedItems.count == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
cell.labelText = array![indexPath.item].frontal
cell.buttonToTrash.setImage(UIImage(named: "Rectangle Empty"), for: .normal)
return cell
}else if trashButtonIsActive == true && selectedItems.count != 0 {
for index in selectedItems {
//I don't know why , but it compares only first element in the array //
if indexPath.item != index {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
cell.labelText = array![indexPath.item].frontal
cell.buttonToTrash.setImage(UIImage(named: "Rectangle Empty"), for: .normal)
return cell
}else{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
cell.labelText = array![indexPath.item].frontal
cell.buttonToTrash.setImage(UIImage(named: "Rectangle Filled"), for: .normal)
return cell
}
}
}else if trashButtonIsActive == false {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
cell.labelText = array![indexPath.item].frontal
cell.buttonToTrash.setImage(nil, for: .normal)
return cell
}

let cellTwo = collectionView.dequeueReusableCell(withReuseIdentifier: "cell hello", for: indexPath)
return cellTwo
}

我希望indexPath.row等于数组中的元素以具有图片“填充矩形”。有人可以帮忙吗?

最佳答案

让你的模型像这样

struct ModelName{
var name: String
var rectangle: Bool
}

那么你所选择的项目的数组将是这样的

var selectedItems = [ModelName]()

然后在 viewdidload 或任何您尝试初始化数据的地方都可以执行此操作

selectedItems.apped(ModelName(name: "name", rectangle: false)
selectedItems.apped(ModelName(name: "new", rectangle: true)
selectedItems.apped(ModelName(name: "newname", rectangle: false)
selectedItems.apped(ModelName(name: "namename", rectangle: false)

等等

然后在 cellforrow 处执行此操作

let item = selectedItems[indexPath.item]
cell.textlabel.text = item.name
if item.rectangle == true{
cell.rectButton.setImage(UIImage(named: "rectangle", for: .normal)
}else{
cell.rectButton.setImage(nil, for: .normal)}
return cell

我尝试过只使用名称和矩形,如果您不想用任何值初始化,则可以做任何您想做的事情,使该变量为零

关于ios - 如何将数组中的元素与indexPath.item进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55754971/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com