我希望你能帮我解决我的代码问题。我正在努力使它在单击一个单元格时检查是否已添加该视图。如果有,则解除视图并重新添加。如果没有,那么它只是添加视图。
这是我目前的代码:
func collectionView(_ collectionView: UICollectionView,
didSelectItemAt indexPath: IndexPath) {
let vc = NoteViewController()
if self.view.subviews.contains(vc.view) {
print("view removed")
}
else {
let cell = collectionView.cellForItem(at: indexPath) as! AnnotatedPhotoCell
sourceCell = cell
vc.picture = resizeImage(image: cell.imageView.image!,
targetSize: CGSize(width:(view.bounds.width - 45),height: 0))
vc.comment = cell.commentLabel
vc.parentVC = parentVC.self
parentVC.self.addChildViewController(vc)
parentVC.self.view.addSubview(vc.view)
vc.didMove(toParentViewController: self)
// 3- Adjust bottomSheet frame and initial position.
let height = view.frame.height
let width = view.frame.width
vc.view.frame = CGRect(x:0, y:self.view.frame.maxY,
width: width, height: height)
}
}
希望你能帮助我。