gpt4 book ai didi

ios - 仅在按下 uitableviewcell 时显示 View

转载 作者:行者123 更新时间:2023-11-29 01:23:25 24 4
gpt4 key购买 nike

我有一个带有 UITableView 和覆盖 View 的 UIViewController 。在 didSelectRowAtIndexPath 中,显示了 View ,但我希望它在用户放开屏幕后立即隐藏。因此,仅当用户按下某个单元格时才会显示该 View ,并在松开后立即隐藏。我尝试使用 touchesEnded 但无法让它工作。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

self.colorView.hidden = false
self.colorView.backgroundColor = colors[indexPath.row]

}

有什么建议吗?

最佳答案

您可以在 TableViewCell 上使用 touchesEnded 方法来检测用户何时结束触摸此单元格。然后使用委托(delegate)将此事件发送到您的 viewcontroller这里例如:

你的TableViewCell.swift

    protocol YourTableViewCellDelegate: class {
func endTouch(indexPath: NSIndexPath)
}


class YourTableViewCell: UITableViewCell {
weak var delegate: YourTableViewCellDelegate?
var indexPath: NSIndexPath

// Add Your Other method
func bind(index: NSIndexPath) {
self.indexPath = index
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.delegate?.endTouch(self.indexPath)
}
}

你的ViewController.swift

class YourViewController: UIViewController, YourTableViewCellDelegate {

// Add Your Other method

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
...
cell.delegate = self
cell.bind(indexPath)
}

// MARK: - YourTableViewCellDelegate
func endTouch(indexPath: NSIndexPath) {
self.colorView.hidden = true
}
}

关于ios - 仅在按下 uitableviewcell 时显示 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34329124/

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