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

标签 ios swift swift2 uicollectionview

我有包含删除按钮和添加的 Collection View

    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 Controller

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
    }

}

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

关于ios - 触发时在 collectionview 单元格中隐藏按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38009043/

相关文章:

ios - 带有 flexbox 的 Bootstrap 列在 iOS 和 Safari 上的宽度不正确

ios - URL 图像不适用于 Swift Playgrounds 中的 UIButton

ios - MKPolyLine 未显示在 Swift 2.0 中的 iOS 应用程序上

iphone - 使用自定义 segue 时 UIActivityIndi​​catorView 卡住

ios - uicollectionviewcell 内的 uitableview 的滚动问题

swift - 使用 Alamofire 的 iOS http POST

ios - 如何在应用程序处于后台时接收 Darwin 通知

ios - 实例变量 NSArray 在 Swift 2 - xCode 7.2 中未正确更新

swift - 在 Swift 中捕获 Objective-C 异常

ios - textFieldShouldReturn - completionHandler 中的一些方法永远不会被调用