在自定义的UITableViewCell攻丝多个按钮斯威夫特
问题描述:
我有一个自定义的UITableViewCell子类在那里我把两个按钮中的细胞,我想既可以点击,但到目前为止,我甚至不能得到任何类型的点击触发(除了行选择,但我禁用了我的UITableViewController子类)。基本上,当选择两个按钮中的一个时,它应该移除这两个按钮并用可选择的选项列表(也包括按钮)更新单元格中的内容。我正在以编程方式执行所有操作(无IB)。在自定义的UITableViewCell攻丝多个按钮斯威夫特
My TableViewCell with Initial Two Buttons
我环顾四周了很多,没有发现任何东西,在一个tableViewCell处理多个按钮。目前,我一直在努力的目标添加到我的按钮在我的UITableViewCell的awakeFromNib():
for button in initialChoiceButtons{
button.isUserInteractionEnabled = true
button.addTarget(self, action: #selector(initialChoicePressed(sender:)), for: UIControlEvents.touchUpInside)
}
有一件事我已经试过在我的tableView在cellForRowAt为我的自定义单元格是把我的按钮前的细胞:
for button in (cell as! FormDropDownTableViewCell).initialChoiceButtons{
cell.bringSubview(toFront: button)
}
我真的很难过,觉得这应该很容易。我正在使用一个stackview内滚动视图的边缘的一切...
答
好吧,所以我想通过创建一个委托协议与我的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"
}
在我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
}
}
现在我添加在cellForRowAt每个按钮的目标,行动在我tableviewcontroller像这样:
button.addTarget(self, action: #selector(handleButtonTaps), for: UIControlEvents.touchUpInside)
和tableviewcontroller中的handleButtonTaps func很简单:
func handleButtonTaps(sender: DelegatingButton){
sender.selectorDelegate?.handleTap(button: sender)
}
喜欢自言自语= P ..
好了,所以我刚刚得到这个与我的多个按钮的工作在tableviewcell由基本目标添加到我的按钮cellForRowAt和移动我的操作方法进入我tableviewcontroller。这里的问题是,我想我的tableviewcell和tableviewcontroller之间要分离的功能。现在为了得到这个工作,我不得不在我的tableviewcontroller的动作函数中执行此操作: (sender.superview!.superview!.superview!.superview as!FormDropDownTableViewCell).showSelectionButtons() –
^^(sender is UIButton的),然后superviews是VerticalStackView,HorizontalStackView,内容查看,UITableViewCell的..必须有一个更好的办法,我可以保持我的实际操作方法我tableviewcell内 –