2016-06-24 120 views
1

我有包含删除按钮,并添加按钮时触发

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) 
     } 
} 

我想,当触发隐藏的添加按钮的CollectionView。我刚刚得到的indexPath的价值,但我不知道如何来隐藏它不刷新的CollectionView

+0

@ funi1234我尝试你的代码,它不工作 –

+0

也许[这](http://stackoverflow.com/a/20297056/5779168)回答将帮助 – tahavath

+0

@tahavath let indexArray = NSArray(object:indexPath!) self.collectionview.reloadItemsAtIndexPaths(indexArray as! [NSIndexPath])我尝试像这样我崩溃可以检查原因 –

回答

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 
    } 

} 

而且你的按钮不见了,当你打他们

+0

谢谢,但它删除所有的单元格时,触发按钮 –

+0

哦对不起,我编辑我的答案:) –

+0

其作品只在右侧的我的collectionview单元格。 –