将单个单元格添加到UiCollectionView

时间:2016-10-20 17:20:04

标签: ios swift xcode uicollectionview

我尝试在UICollectionViewCell的末尾创建自定义单元格。最后一项应该是"自定义单元格"所以用户可以添加另一条记录。尝试了一些解决方案但没有成功。到目前为止,我的代码似乎是这样的:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! EquipamentosCollectionViewCell
        if (indexPath as NSIndexPath).row == 0 {
            cell.imgEquip?.image = UIImage(named: "novavisita")
            return cell
        }else{
            cell.backgroundColor = UIColor.green
            return cell
        }
}

我也试过了this solution,但这并不是我想要的方式。

问题在于它取代了第一个。

有关如何做到这一点的任何建议吗?

1 个答案:

答案 0 :(得分:0)

然后最后添加该单元格:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! EquipamentosCollectionViewCell
    if (indexPath as NSIndexPath).item == YOUR_COLLECTION.count - 1{
        //Add New Record Cell
        cell.imgEquip?.image = UIImage(named: "novavisita")
        return cell
    }else{
        cell.backgroundColor = UIColor.green
        return cell
    }
}