2016-06-24 118 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單元格。 –