Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 删除单元格内的子视图_Swift - Fatal编程技术网

Swift 删除单元格内的子视图

Swift 删除单元格内的子视图,swift,Swift,在我的ViewController中,我有一个变量: var shareButtonTapped = false 我希望当我单击导航栏上的共享按钮时,它会显示所有单元格的共享按钮,除了indexath.row==0的单元格 以下是shareButton的操作: @IBAction func shareButtonTapped(sender: AnyObject) { shareButtonTapped = !shareButtonTapped collectionView.re

在我的
ViewController
中,我有一个变量:

var shareButtonTapped = false
我希望当我单击
导航栏上的
共享按钮
时,它会显示所有单元格的共享按钮,除了indexath.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
        }

    }
它奏效了:

但我想再次单击
NavigationBar
上的
shareButton
,每个单元格内的
shareButton
都将消失。如何做到这一点


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

else
块中使用
removeFromSuperview()
方法:

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

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

假设您的按钮名为shareImage,然后在cellForRow中

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

谢谢你的帮助。谢谢你的帮助。删除和添加子视图不是一个好方法,除非这是唯一的选择。很好。我试过了。这更好,因为我有保存和共享按钮。