如何使用indexPath.item比较数组中的元素?

时间:2019-04-18 23:44:41

标签: ios swift uicollectionview

当我使用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具有图片“ Rectangle Filled”。谁能提供帮助?

1 个答案:

答案 0 :(得分:0)

像这样制作模型

var selectedItems = [ModelName]()

然后您选择的项目的数组将像这样

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)

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

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

然后进行cellforrow

 constructor(private data: DataService) {
         zip(this.data.getuserdetails(),this.data.getposts()).subscribe((results)=> 
          {
             this.dataItem = results[0];
             this.post = results[1];
          });

        });


       });

我只用名称和矩形进行了尝试,如果您不想使用任何值进行初始化,则可以执行所需的任何操作