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

Posted

技术标签:

【中文标题】触发时隐藏collectionview单元格中的按钮【英文标题】:hide button in a collectionview cell when trigger 【发布时间】:2016-06-24 08:38:56 【问题描述】:

我有包含 del 按钮并添加的 collectionview

    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 的情况下隐藏它

【问题讨论】:

@funi1234 我试试你的代码它不起作用 也许this 回答会有所帮助 @tahavath let indexArray = NSArray(object: indexPath!) self.collectionview.reloadItemsAtIndexPaths(indexArray as! [NSIndexPath]) 我这样尝试我遇到崩溃可以检查原因 错误是什么? 现在可以了,我没有收到错误但什么也没发生按钮没有隐藏@tahavath 【参考方案1】:

创建自定义单元格

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
    


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

【讨论】:

谢谢,但它会在触发按钮时删除所有单元格 它只适用于我的收藏视图单元格的右侧。 右边是什么意思?你能更新你的问题吗? 我使用像 pinterest board 这样的 collectionview,但是当我检查 indexpath 时,他们有相同的 indexPath

以上是关于触发时隐藏collectionview单元格中的按钮的主要内容,如果未能解决你的问题,请参考以下文章

普通 CollectionView 单元格中的动态 CollectionView 单元格

多个tableView单元格中的多个collectionView

如何快速更新消息 CollectionView 单元格中的数据?

如何将手势识别器添加到collectionview单元格中的uiview

collectionview 单元格中的阴影与 swift 中的其他单元格的行为方式不同

collectionView 中的图像在滚动时发生变化