gpt4 book ai didi

ios - UITableViewCell 子类中的 UIButton 未注册水龙头

转载 作者:行者123 更新时间:2023-11-28 09:12:15 25 4
gpt4 key购买 nike

我有一个收藏夹按钮,我希望能够点击该按钮以将单元格标记为收藏夹。该单元格也是可点击的。

这是 UITableViewCell 的子类。我已经添加了按钮,但我无法让它调用目标操作。换句话说,我点击单元格中的按钮,但没有任何反应:

class ServerSelectionTableViewCell: UITableViewCell {

let isSelected = UIImage(named: "icon.png")
let unselected = UIImage(named: "star.png")
var favoriteButton: UIButton? = nil

var server : Server? = nil {
didSet{
if let theServer = server {
var server : String? = theServer.serverCode
self.textLabel?.text = serverCode
server?.isActive() { success in
self.accessoryType = success ? .Checkmark : .None
}
}
}
}

required init(coder aDecoder: NSCoder!) {
super.init(coder: aDecoder)
}

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: .Subtitle, reuseIdentifier: reuseIdentifier)
self.detailTextLabel?.textColor = UIColor.grayColor()
self.favoriteButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
self.favoriteButton?.setImage(unselected, forState: UIControlState.Normal)
self.favoriteButton?.setImage(isSelected, forState: UIControlState.Selected)
self.favoriteButton?.addTarget(self, action: Selector("buttonAction:"), forControlEvents: UIControlEvents.TouchDown)

setTranslatesAutoresizingMaskIntoConstraints(false)

self.favoriteButton?.userInteractionEnabled = true
self.contentView.addSubview(self.favoriteButton!)
}

func buttonAction(sender:UIButton!) {
println("Button tapped")
}

override func layoutSubviews() {
super.layoutSubviews()

self.favoriteButton?.frame = CGRectMake(10, 5, 35, 35)
self.textLabel?.frame = CGRectMake(55, 5, self.bounds.width, self.bounds.height)
}

override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

// Configure the view for the selected state
}

}

永远不会调用函数 buttonAction。我在模拟器和设备上都试过了,我可以看到当我点击最喜欢的按钮时,单元格没有被选中,但函数仍然没有被调用。

编辑:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let reuseId = "cell"
tableView.registerClass(ServerSelectionTableViewCell.self, forCellReuseIdentifier: reuseId)
var cell = tableView.dequeueReusableCellWithIdentifier(reuseId) as ServerSelectionTableViewCell

if let servers = getServers() {
let server = servers[indexPath.row]

cell.server = server
}

return cell
}

override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
if let servers = getServers() {
let server = servers[indexPath!.row]
var cell = tableView.cellForRowAtIndexPath(indexPath) as ServerSelectionTableViewCell

println("Server selected \(server)")
}
}

最佳答案

所以看起来按钮在注册点击,但是行

println("点击按钮")

实际上并没有打印到控制台,所以我认为它不是。

关于ios - UITableViewCell 子类中的 UIButton 未注册水龙头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28920910/

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