2016-04-23 104 views
1

子视图在我ViewController,我有一个变量:取出内细胞

var shareButtonTapped = false 

我想,当我点击shareButtonnavigationBar,它会显示shareButton所有的细胞,除了电池,其indexPath .row == 0,

这里是shareButton动作:

@IBAction func shareButtonTapped(sender: AnyObject) { 
    shareButtonTapped = !shareButtonTapped 
    collectionView.reloadData() 
} 

这里是CollectionViewDataSource方法:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

     let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! GiftCollectionViewCell 

     if shareButtonTapped { 

      cell.gift = gifts[indexPath.row] 
      let shareButton = FBSDKShareButton(frame: CGRect(x: 0, y: 0, width: 80, height: 30)) 
      shareButton.tag = 1 
      let photo = FBSDKSharePhoto() 
      photo.image = gifts[indexPath.row].image 
      photo.userGenerated = true 
      let content = FBSDKSharePhotoContent() 
      content.photos = [photo] 
      content.contentURL = NSURL(string: "http://www.fantageek.com") 
      shareButton.shareContent = content 

      cell.contentView.addSubview(shareButton) 
      return cell 
     } else { 
      cell.gift = gifts[indexPath.row] 
      return cell 
     } 

    } 

它的工作:

enter image description here

但我想再次点击此shareButtonNavigationBar,所有的shareButton内的每个细胞就会消失。这个怎么做?

任何帮助,将不胜感激。谢谢。

回答

1

使用removeFromSuperview()方法您else块内:

else { 
    cell.gift = gifts[indexPath.row] 
    for subview in cell.contentView.subviews { 
     if subview is FBSDKShareButton { 
      subview.removeFromSuperview() 
     } 
    } 
    return cell 
} 
+0

感谢您帮助。 – Khuong

1

更好的方法是保持共享按钮永远是细胞的组成部分。并基于bar按钮的shareButtonTapped状态显示并隐藏该按钮。

比方说,你的按钮名称为shareImage然后cellForRow

cell.shareButton.hidden = false 
if(shareButtonTapped){ 
    cell.shareButton.hidden = true 
} 
+0

感谢您的帮助。 – Khuong

+0

删除和添加子视图不是一种好方法,除非这是唯一的选择。 –

+0

不错。我试过了。这更好,因为我同时保存和分享按钮。 – Khuong