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