Ios 触发时在collectionview单元格中隐藏按钮

Ios 触发时在collectionview单元格中隐藏按钮,ios,swift,swift2,uicollectionview,Ios,Swift,Swift2,Uicollectionview,我有collectionview,其中包含del按钮和add cell.coupon_add.tag = indexPath.row cell.coupon_add?.layer.setValue(id, forKey: "coupon_id") cell.coupon_add?.layer.setValue(uID, forKey: "user_id") cell.coupon_add?.addTarget(self, action: #selector(Vie

我有collectionview,其中包含del按钮和add

    cell.coupon_add.tag = indexPath.row
    cell.coupon_add?.layer.setValue(id, forKey: "coupon_id")
    cell.coupon_add?.layer.setValue(uID, forKey: "user_id")
    cell.coupon_add?.addTarget(self, action: #selector(ViewController.addItem(_:)), forControlEvents: UIControlEvents.TouchUpInside)

  func addItem(sender:UIButton) {
    let point : CGPoint = sender.convertPoint(CGPointZero, toView:collectionview)
    let indexPath = collectionview!.indexPathForItemAtPoint(point)
    let cell = collectionview.dequeueReusableCellWithReuseIdentifier("listcell", forIndexPath: indexPath!) as! ListCell

    let coupon_id : String = (sender.layer.valueForKey("coupon_id")) as! String
    let user_id : String = (sender.layer.valueForKey("user_id")) as! String
        if user_id == "empty" {
            self.login()
        }else{
            print("adding item**",indexPath)

            cell.coupon_add.hidden = true
            cell.coupon_del.hidden = true
            let buttonRow = sender.tag
            print(buttonRow)
        }
}

我想在触发时隐藏添加按钮。我只得到indexPath的值,但我不知道如何在不刷新collectionview的情况下隐藏它

class CustomCell: UICollectionViewCell {

    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var delButton: UIButton!
    @IBOutlet weak var addButton: UIButton!

    @IBAction func addTapped(sender: AnyObject) {
       delButton.removeFromSuperview()
       addButton.removeFromSuperview()
    }
}
典型的CollectionView控制器

class ViewController: UICollectionViewController {

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10;
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CustomCell

        cell.label.text = "Cell \(indexPath.row)"

        return cell
    }

}

当你点击按钮时,按钮就会消失,创建一个自定义单元格

class CustomCell: UICollectionViewCell {

    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var delButton: UIButton!
    @IBOutlet weak var addButton: UIButton!

    @IBAction func addTapped(sender: AnyObject) {
       delButton.removeFromSuperview()
       addButton.removeFromSuperview()
    }
}
典型的CollectionView控制器

class ViewController: UICollectionViewController {

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10;
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CustomCell

        cell.label.text = "Cell \(indexPath.row)"

        return cell
    }

}

当你按下按钮时,你的按钮就会消失。

@funi1234我试试你的代码,它不起作用,也许答案会help@tahavath让indexArray=NSArray(对象:IndexXPath!)self.collectionview.reloadItemsAtIndexPaths(indexArray为![NSIndexPath])我试着这样做,我可以检查原因错误是什么?现在没问题,我没有收到错误,但什么都没有发生按钮没有隐藏@tahavath@funi1234我试试你的代码,它不起作用,也许答案会help@tahavath让indexArray=NSArray(对象:IndexXPath!)self.collectionview.reloadItemsAtIndexPaths(indexArray为![nsindepath])我试着这样做,我得到崩溃可以检查原因错误是什么?现在没问题,我没有得到错误,但什么都没有发生按钮没有隐藏@TahavathThank,但它会在触发时删除所有单元格按钮只在我的collectionview单元格的右侧工作。右侧是什么意思?你能更新你的问题吗?我使用collectionview与pinterest board相似,但当我检查indexpath时,它们具有相同的indexpath谢谢,但它会在触发按钮时删除所有单元格。按钮仅在我的collectionview单元格的右侧起作用。右侧是什么意思?你能更新你的问题吗?我使用的collectionview与pinterest board相似,但当我检查indexpath时它们具有相同的属性