我有包含删除按钮和添加的 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/