【问题标题】:Adding custom button in custom table view cell , and at a time only one button of cell should be selected in swift, could anyone help me out for this?在自定义表格视图单元格中添加自定义按钮,一次只能快速选择一个单元格按钮,有人可以帮我解决这个问题吗?
【发布时间】:2016-12-12 07:26:10
【问题描述】:

请查看我的自定义 UITableview 单元格的代码,我遇到了这个问题,因为 tableview 委托确实没有调用 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()
}

}

我想在自定义表格视图单元格中添加自定义按钮,一次只能快速选择一个单元格按钮,有人可以帮我吗?

谢谢

【问题讨论】:

  • 你能贴一张你的单元格的截图吗?您的问题是 didSelect 没有被调用还是单元格内的多个按钮被选中?
  • 如果您的问题是由于 didselect 方法没有被调用,那么请确保您的代理符合要求,并从故事板检查您的选择在 TableView 中设置为“单选”。跨度>
  • @Nisha 让它也包含在您填充的 ​​tableview 屏幕截图中。
  • 谢谢大家,我得到了解决方案。我获取了uitableview的所有可见单元格并取消选择了所有可见单元格。

标签: ios objective-c swift uitableview radio-button


【解决方案1】:

看到这一行 cell.radioButton1.addTarget(self, action: #selector(DpMarginView.buttonTapped(_:)), forControlEvents: .TouchUpInside) 不应该出现在 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. 为什么你将tableViewCell 子类中radioButton1 的标签设置为101,而你将它更改为viewController 子类中的indexPath.row?整个设置标签的东西在这里没有任何用途。

【讨论】:

    猜你喜欢
    • 2019-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多