请查看我的自定义uitaiteView单元格代码,我面临问题,因为tableView dod dod dod dod select方法未调用。
我的代码在这里:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(self.cellIdentifier, forIndexPath: indexPath) as! DpMarginRptTableViewCell
cell.selectionStyle = .None
if dataSource.count > 1
{
let dpMarginModel : DPSRDPDetailModel = self.dataSource.objectAtIndex(indexPath.row) as! DPSRDPDetailModel
cell.scriptLabel.text = "(dpMarginModel.scripCode)"
cell.dpQtyLabel.text = "(dpMarginModel.dpBalanceAC)"
cell.haricutLabel.text = String(format: "%.2f",Float(dpMarginModel.valueAfterHairCut)/100) //"(dpMarginModel.valueAfterHairCut)"
cell.transferTextfield.tag = indexPath.row
cell.transferTextfield.text = "(dpMarginModel.transferText)"
cell.transferTextfield.delegate = self
cell.transferTextfield.layer.borderWidth = 1.0
cell.transferTextfield.layer.borderColor = UIColor.blackColor().CGColor
cell.radioButton1.tag = indexPath.row
cell.radioButton1.addTarget(self, action: #selector(DpMarginView.buttonTapped(_:)), forControlEvents: .TouchUpInside)
cell.radioButton1.setTitle(String.iconWithName(.radioUnchecked), forState: UIControlState.Normal)
cell.radioButton1.setTitle(String.iconWithName(.radioChecked), forState: UIControlState.Selected)
}
}
CustomTableViewCell类代码
class CustomTableViewCell : UITableViewCell {
var screenWidth:CGFloat!
var screenHeight:CGFloat!
var radioButton1 : UIButton!
var scriptLabel : UILabel!
var dpQtyLabel:UILabel!
var haricutLabel:UILabel!
var transferTextfield: UITextField!
override init(style: UITableViewCellStyle, reuseIdentifier: String?)
{
super.init(style: style, reuseIdentifier: reuseIdentifier)
let screenSize:CGRect = UIScreen.mainScreen().bounds
self.screenWidth = screenSize.width
self.screenHeight = screenSize.height
let boldFont = UIFont(name: "Helvetica Neue", size: 11)?.fontDescriptor().fontDescriptorWithSymbolicTraits(UIFontDescriptorSymbolicTraits.TraitBold)
radioButton1 = UIButton(frame: CGRectMake(0, 5, 30, 40))
radioButton1.tag = 101
radioButton1.titleLabel?.font = UIFont(name: "Icomoon", size: 18)
radioButton1.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
radioButton1.setTitleColor(UIColor.blackColor(), forState: UIControlState.Selected)
self.scriptLabel = UILabel.init(frame: CGRectMake(radioButton1.frame.size.width + 5, 5,(screenWidth/4) , 40))
self.scriptLabel.textAlignment = .Left
self.scriptLabel.font = UIFont(descriptor: boldFont!, size: 11)
self.dpQtyLabel = UILabel.init(frame: CGRectMake(scriptLabel.frame.origin.x + scriptLabel.frame.size.width, 5, (screenWidth/4), 40))
self.dpQtyLabel.textAlignment = .Left
self.dpQtyLabel.font = UIFont(name: "HelveticaNeue", size: 11)
self.haricutLabel = UILabel.init(frame: CGRectMake(dpQtyLabel.frame.origin.x + dpQtyLabel.frame.size.width, 5, (screenWidth/4), 40))
self.haricutLabel.textAlignment = .Left
self.haricutLabel.font = UIFont(name: "HelveticaNeue", size: 11)
self.transferTextfield = UITextField.init(frame: CGRectMake((haricutLabel.frame.origin.x + haricutLabel.frame.size.width)-20, 5, (screenWidth/4) - 20, 40))
self.transferTextfield.textAlignment = .Center
self.transferTextfield.font = UIFont(name: "HelveticaNeue", size: 11)
self.transferTextfield.keyboardType = .NumberPad
self.contentView.addSubview(self.radioButton1)
self.contentView.addSubview(self.scriptLabel)
self.contentView.addSubview(self.dpQtyLabel)
self.contentView.addSubview(self.haricutLabel)
self.contentView.addSubview(self.transferTextfield)
self.contentView.backgroundColor = UIColor.clearColor()
}
}
我想在自定义表视图单元格中添加自定义按钮,并且一次只能在Swift中选择一个单元按钮,任何人都可以帮助我吗?
谢谢
请参阅此行 cell.radiobutton1.addtarget(self,action:#selector(dpmarginview.buttontapped(_ :)),forcontrolevents:.touchupinside)t在CellForrowatIndExpath中存在。相反,当您创建该按钮时,您应该将该行放入 customTableViewCell 中。并在CustomTableViewCell本身中调用 buttontapped 操作。
然后,您需要使用委托方法将消息传递给包含此tableView的UiviewVontroller。
因此,您的CustomTableViewCell会这样开始: -
protocol CustomDelegate: class {
func buttonTouchedAction()
}
class CustomTableViewCell : UITableViewCell {
weak var delegate: CustomDelegate?
var screenWidth:CGFloat!
var screenHeight:CGFloat!
.
.
.
}
在您的CellForrowatIndExpath中,而不是添加目标,而是使用此代码: -
cell.delegate = self
,在您的ViewController的底部添加此。我假设您的ViewController名称是CustomViewController: -
extension CustomViewController: CustomDelegate {
fun buttonTouchedAction() {
// Do your action here
}
}
nb。为什么在将其更改为indexpath.row中的tableViewCell子类中的radiobutton1为101的标签。整个设置标签的东西在这里没有任何目的。