删除Cell中的subView

时间:2016-04-23 05:42:26

标签: swift

在我的var shareButtonTapped = false 中,我有一个变量:

shareButton

我希望当我点击navigationBar上的shareButton时,它会显示所有单元格的shareButton,但indexPath.row == 0的单元格除外。

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

的操作
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
        }

    }

这是CollectionViewDataSource方法:

shareButton

有效:

enter image description here

但我想再次点击NavigationBar上的shareButton,每个小区内的所有layout-land都会消失。怎么做?

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

更好的方法是保持共享按钮始终是单元格的一部分。并根据shareButtonTapped栏按钮的状态显示和隐藏按钮。

让我们说你的按钮名称是shareImage然后是cellForRow

cell.shareButton.hidden = false
if(shareButtonTapped){
    cell.shareButton.hidden = true
}
相关问题