我在自定义单元格中有一个按钮,当我点击它时它没有做任何事情。我故意尝试错误地输入addTarget方法,因此它会崩溃,所以我可以得到按钮被调用的确认,但应用程序不会崩溃。为什么没有发生?下面是自定义单元格和tablecell的代码。
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])
}
答案 0 :(得分:0)
我认为问题在于你在没有冒号的情况下调用了游戏音乐。由于sender参数,需要冒号。
编译器正在尝试使用此名称调用方法:
func playmymusic()
而不是真正的:
func playmymusic(sender: UIButton!)
所以只需尝试在行动结束时添加冒号(:),如下所示:
cell.playbutton.addTarget(self, action: "playmymusic:", forControlEvents: .TouchUpInside)
这是Objective-c调用方法(发送消息)的遗产,我知道它有点令人困惑。