触发时隐藏集合视图单元格中的按钮

时间:2016-06-24 08:38:57

标签: ios swift swift2 uicollectionview

我的collectionview包含del按钮并添加

    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

1 个答案:

答案 0 :(得分:0)

创建自定义单元格

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
    }

}

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

相关问题