gpt4 book ai didi

xcode - 隐藏 UICollectionViewCell 中的 View

转载 作者:行者123 更新时间:2023-11-30 13:25:58 28 4
gpt4 key购买 nike

谁能告诉我它不会在模拟器中隐藏我的 UIView 吗?该代码正在运行,并且打印了我添加到函数中的数字,但它仍然不起作用。

基本上我想点击单元格,单元格中的 greyView 应该消失。

 override func viewDidLoad() {
super.viewDidLoad()

let lpgr = UITapGestureRecognizer(target: self, action: #selector(ViewController.handleTap(_:)))
lpgr.numberOfTapsRequired = 1
lpgr.delaysTouchesBegan = true
lpgr.delegate = self
AlarmCollection.addGestureRecognizer(lpgr)

}


// MARK: UICollectionViewCell

func handleTap(gestureReconizer: UITapGestureRecognizer) {
if gestureReconizer.state != UIGestureRecognizerState.Ended {
return
}

let p = gestureReconizer.locationInView(AlarmCollection)
let indexPath = AlarmCollection.indexPathForItemAtPoint(p)

if let index = indexPath {
var cell = AlarmCollection.cellForItemAtIndexPath(index)
// do stuff with your cell, for example print the indexPath
print(index.row)
updateView(indexPath!)
} else {
print("Could not find index path")
}
}



func updateView(indexPath: NSIndexPath) {
let cell = AlarmCollection.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! AlarmItemCell
cell.greyView.hidden = true
print("2")

}

编辑:

// MARK: UICollectionViewCell



func handleTap(gestureReconizer: UITapGestureRecognizer) {
if gestureReconizer.state != UIGestureRecognizerState.Ended {
return
}

let p = gestureReconizer.locationInView(AlarmCollection)
let indexPath = AlarmCollection.indexPathForItemAtPoint(p)

if let index = indexPath {
var cell = AlarmItemCell()
// do stuff with your cell, for example print the indexPath
print(index.row)
updateView()
} else {
print("Could not find index path")
}
}




func updateView(cell: UICollectionViewCell) {
if let cell = cell as? AlarmItemCell {
cell.greyView.hidden = true
}
print("2")
}

编辑2:

if let index = indexPath {
let cell = AlarmCollection.cellForItemAtIndexPath(index)
updateView(cell!)


}

最佳答案

您应该能够使用从 Collection View 中获取的单元格来引用 greyView。

例如,将您的 updateView 函数更改为:

func updateView(cell: UICollectionViewCell) {
if let cell = cell as? AlarmItemCell {
cell.greyView.hidden = true
}
print("2")
}

然后将您在此处获得的单元格var cell = AlarmCollection.cellForItemAtIndexPath(index)传递给该函数。据我所知,您现在执行此操作的方式并没有捕获正确的单元格。

编辑:

if let index = indexPath, 
let cell = AlarmCollection.cellForItemAtIndexPath(index) {
updateView(cell)
} else {
print("Could not find index path or cell")
}

关于xcode - 隐藏 UICollectionViewCell 中的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37190722/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com