gpt4 book ai didi

ios - 自定义附件 View 按钮的操作是什么 - swift

转载 作者:搜寻专家 更新时间:2023-10-31 08:13:12 26 4
gpt4 key购买 nike

这似乎在 swift 和 objc 中被问过几次,但我看不到 swift 的正确答案所以希望这次有人能帮助我。我创建了一个自定义附件 View 按钮,但需要正确的按钮操作:因为“accessoryButtonTapped”是一个无法识别的选择器。

调用 tableViewCellDelegate 方法 willSelectRowAtIndexPath 所需的选择器是什么?

我的代码是:

let cellAudioButton = UIButton(type: .Custom)
cellAudioButton.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
cellAudioButton.addTarget(self, action: "accessoryButtonTapped", forControlEvents: .TouchUpInside) //INCORRECT ACTION:
cellAudioButton.setImage(UIImage(named: "blueSpeaker.png"), forState: .Normal)
cellAudioButton.contentMode = .ScaleAspectFit
cell.accessoryView = cellAudioButton as UIView

在此先感谢任何可以提供帮助的人。

最佳答案

为什么需要调用willSelectRowAtIndexPath?您已正确完成所有操作,要解决您的 unrecognized selector 错误,只需创建一个函数,当您点击 cell.accessoryView 时将调用该函数。在你的情况下:

func accessoryButtonTapped(){
print("Tapped")
}

更新
如果你想获得 indexPath,你可以

将标签添加到您的cellAudioButton:

cellAudioButton.tag = indexPath.row

在你的addTarget中添加一个:来传递一个参数

在你的函数中

func accessoryButtonTapped(sender : AnyObject){
print(sender.tag)
print("Tapped")
}

所以整个代码:

let cellAudioButton = UIButton(type: .Custom)
cellAudioButton.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
cellAudioButton.addTarget(self, action: "accessoryButtonTapped:", forControlEvents: .TouchUpInside)
cellAudioButton.setImage(UIImage(named: "blueSpeaker.png"), forState: .Normal)
cellAudioButton.contentMode = .ScaleAspectFit
cellAudioButton.tag = indexPath.row
cell.accessoryView = cellAudioButton as UIView

func accessoryButtonTapped(sender : AnyObject){
print(sender.tag)
print("Tapped")
}

关于ios - 自定义附件 View 按钮的操作是什么 - swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34778283/

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