gpt4 book ai didi

swift - 快速禁用 tableview 单元格按钮

转载 作者:行者123 更新时间:2023-11-28 16:02:33 27 4
gpt4 key购买 nike

我的按钮工作正常,但我不知道如何在点击时禁用它。我不确定我是否可以从 addSomething(sender: UIButton) 函数中引用它,就像我引用 sender.tag 一样。任何想法?感谢您的帮助。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let myCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! ExploreCell

// Configure the cell...
myCell.configureCell(teams[indexPath.row])

myCell.addSomethingButton.tag = indexPath.row
myCell.addSomethingButton.addTarget(self, action: #selector(self.addSomething), forControlEvents: .TouchUpInside)

myCell.addSomethingButton.enabled = true

//disable cell clicking
myCell.selectionStyle = UITableViewCellSelectionStyle.None

return myCell
}

最佳答案

你需要做的是将所有被点击的按钮存储在一个数组中,检查这个标签(当前indexPath.row)的按钮是否被点击:

class ViewController: UIViewController {
var tappedButtonsTags = [Int]()

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let myCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! ExploreCell

// Configure the cell...
myCell.configureCell(teams[indexPath.row])

myCell.addSomethingButton.tag = indexPath.row

// here is the check:
if tappedButtonsTags.contains(indexPath.row) {
myCell.addSomethingButton.enabled = false
} else {
myCell.addSomethingButton.addTarget(self, action: #selector(self.addSomething), forControlEvents: .TouchUpInside)
myCell.addSomethingButton.enabled = true
}

//disable cell clicking
myCell.selectionStyle = UITableViewCellSelectionStyle.None

return myCell
}

// I just Implemented this for demonstration purposes, you can merge this one with yours :)
func addSomething(button: UIButton) {
tappedButtonsTags.append(button.tag)
tableView.reloadData()
// ...
}
}

希望对您有所帮助。

关于swift - 快速禁用 tableview 单元格按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40793709/

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