我在自定义单元格中有一个按钮,当我单击它时,它不会做任何事情。我试图故意输入错误的addTarget方法,这样它就会崩溃,这样我就可以确认按钮正在被调用,但应用程序不会崩溃。为什么什么都没发生?下面是自定义单元格和表格单元格的代码。

class ProfileMusicCell: UITableViewCell {
@IBOutlet weak var customtitle: UILabel!
@IBOutlet weak var customartist: UILabel!
@IBOutlet weak var playbutton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}下面是表视图的代码
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = table.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! ProfileMusicCell
cell.customtitle.text = ret[indexPath.row]
cell.customartist.text = ter[indexPath.row]
cell.customtitle.font = UIFont(name: "Lombok", size: 22)
cell.customtitle.textColor = UIColorFromRGB("4A90E2")
cell.customartist.font = UIFont(name: "Lombok", size: 16)
cell.customartist.textColor = UIColor.blackColor()
cell.playbutton.tag = indexPath.row
//I am purposely leaving out the : so the app can crash, but it is not crashing.
cell.playbutton.addTarget(self, action: "playmymusic", forControlEvents: .TouchUpInside)
}
func playmymusic(sender: UIButton!) {
let playButtonrow = sender.tag
print(ret[playButtonrow])
print(ter[playButtonrow])
}发布于 2015-11-12 03:23:43
我认为问题在于您调用的playmymusic没有冒号。由于发送者参数的原因,冒号是必需的。
编译器正在尝试调用具有此名称的方法:
func playmymusic() 而不是真正的:
func playmymusic(sender: UIButton!)因此,只需尝试在操作的末尾添加冒号(:),如下所示:
cell.playbutton.addTarget(self, action: "playmymusic:", forControlEvents: .TouchUpInside)这是Objective-c调用方法(发送消息)的一种遗留方式,我知道这有点令人困惑。
https://stackoverflow.com/questions/33657560
复制相似问题