[英]Tapping Multiple Buttons in Custom UITableViewCell Swift
I have a custom UITableViewCell subclass where I am putting two buttons in the cell, I want both to be clickable, but so far I cannot even get any kind of click to trigger (besides row selection, but I disabled that on my UITableViewController subclass).我有一个自定义的 UITableViewCell 子类,我在单元格中放置了两个按钮,我希望这两个按钮都是可点击的,但到目前为止我什至无法触发任何类型的点击(除了行选择,但我在 UITableViewController 子类中禁用了它) . Basically When one of two buttons is selected, it should remove those two buttons and update what is in the cell with a list of selectable choices (also buttons).
Basically When one of two buttons is selected, it should remove those two buttons and update what is in the cell with a list of selectable choices (also buttons). I am doing everything programmatically (no IB).
我正在以编程方式做所有事情(没有 IB)。
My TableViewCell with Initial Two Buttons我的 TableViewCell 带有最初的两个按钮
I have looked around a lot and haven't found anything that handles more than ONE button in a tableViewCell.我环顾四周,没有发现任何可以处理 tableViewCell 中一个以上按钮的东西。 Currently I've been trying to add targets to my buttons in my UITableViewCell's awakeFromNib():
目前,我一直在尝试向 UITableViewCell 的awakeFromNib() 中的按钮添加目标:
for button in initialChoiceButtons{
button.isUserInteractionEnabled = true
button.addTarget(self, action: #selector(initialChoicePressed(sender:)), for: UIControlEvents.touchUpInside)
}
One thing I've tried is in my tableView in cellForRowAt for my custom cell is to bring my buttons to the front of the cell:我尝试过的一件事是在 cellForRowAt 中的 tableView 中为我的自定义单元格将我的按钮带到单元格的前面:
for button in (cell as! FormDropDownTableViewCell).initialChoiceButtons{
cell.bringSubview(toFront: button)
}
I'm really stumped and feel like this should easy.我真的很难过,觉得这应该很容易。 I'm on the verge of just using a stackView inside of scrollview for everything...
我即将在滚动视图中使用 stackView 来处理所有事情......
Ok so I figured out a somewhat clean way to separate button taps in my tableviewcell, by creating a delegate protocol with a function that I'll call from my target in my tableviewcontroller for every button in my tableviewcell.好的,所以我想出了一种比较干净的方法来分离我的 tableviewcell 中的按钮点击,通过创建一个具有函数的委托协议,我将从我的 tableviewcontroller 中的目标为我的 tableviewcell 中的每个按钮调用该函数。
protocol UIButtonSelectorDelegate{
func handleTap(button:DelegatingButton) //will select different functions based on DelegatingButton.actionType
}
class DelegatingButton:UIButton{
var selectorDelegate:UIButtonSelectorDelegate?
var actionType:String = "default"
}
in my FormDropDownTableViewCell I conform to the UIButtonSelectedDelegate and implement handleTap like so:在我的 FormDropDownTableViewCell 中,我符合 UIButtonSelectedDelegate 并像这样实现 handleTap:
func handleTap(button:DelegatingButton){
switch button.actionType{
case "initialChoiceSelect":
initialChoicePressed(initialChoice:button) //specific method for certain button.actionType also in my FormDropDownTableViewCell
case "cardTypeSelect":
cardTypeSelected(selectedCardType:button)
default:
break
}
}
Now I add the target-actions for every button in cellForRowAt in my tableviewcontroller like so:现在,我在 tableviewcontroller 中为 cellForRowAt 中的每个按钮添加目标操作,如下所示:
button.addTarget(self, action: #selector(handleButtonTaps), for: UIControlEvents.touchUpInside)
and the handleButtonTaps func in the tableviewcontroller is simple: tableviewcontroller 中的 handleButtonTaps 函数很简单:
func handleButtonTaps(sender: DelegatingButton){
sender.selectorDelegate?.handleTap(button: sender)
}
Enjoyed Talking to myself =P ..喜欢自言自语=P ..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.