【问题标题】:how to pass bool value from table view cell class to table view class in swift 3?如何在swift 3中将布尔值从表格视图单元格类传递给表格视图类?
【发布时间】:2017-09-07 05:21:19
【问题描述】:

在这里,我正在使用委托将我的布尔值从表格视图单元格传递到表格视图类,但无法传递任何人都可以帮助我如何使用任何其他方法传递,或者如果不纠正我在传递它时出了什么问题,那么它返回零值?

这是我的表格视图类的代码

 class CheckoutViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,checkoutDelegate,UIGestureRecognizerDelegate,boolValidationDelegate {
 var radio: Bool?
 func boolvalidation(bool: Bool)
    {
        radio = bool
    }
 @IBAction func continueButtonAction(_ sender: Any) {
        print(radio)
        if radio == false {
            let radiobutton = SCLAlertView()
            _ =  radiobutton.showError("Warning", subTitle: "Please select shipping method", closeButtonTitle: "OK")
        }
        else {
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let addtoCartVC = storyboard.instantiateViewController(withIdentifier: "payment") as! PaymentMethodViewController
            self.navigationController?.pushViewController(addtoCartVC, animated: true)
        }
    }
 let cell = tableView.dequeueReusableCell(withIdentifier: "shippingmethodcell", for: indexPath) as! MethodTableViewCell
            cell.delegate = self
            return cell

这是我的表格视图单元格类

protocol boolValidationDelegate{
    func boolvalidation(bool: Bool)
}

class MethodTableViewCell: UITableViewCell,UITableViewDataSource,UITableViewDelegate {
 var radioSelection: Bool?
 var boolDelegate: boolValidationDelegate?
func paymentRadioAction(button : KGRadioButton) {
        _ = button.center
        let centralPoint = button.superview?.convert(button.center, to:self.shippingTableView)
        let indexPath =  self.shippingTableView.indexPathForRow(at: centralPoint!)
        if button.isSelected {

        } else{
            chekIndex = indexPath
            radioSelection = true
            self.shippingTableView.reloadData()
        }
        self.boolDelegate?.boolvalidation(bool: radioSelection!)
        print(radioSelection)
    }

【问题讨论】:

  • 单元格。 boolDelegate = self 在 CheckoutViewController 中
  • 设置boolDelegate 后,就像下面的答案所建议的那样,您应该注意您有一个强大的参考周期。你真的想让你的协议成为class 协议(例如protocol boolValidationDelegate: class { ... }),然后让你的boolDelegate 成为weak 属性。 (我还将协议重命名为以大写字母开头(例如BoolValidationDelegate),就像遵循标准编程约定一样。)
  • 发布代码如何在 cellForRowAtIndexPath 中设置委托
  • 按照你的要求发布了上面的代码@raki
  • @VamsiKrishnaS 您在 tableview cellForRowAt 方法中添加此行 cell.boolDelegate = self 的位置?

标签: ios uitableview swift3


【解决方案1】:

您需要将tableView(_:cellForRowAt:) 中的cell.boolDelegate 设置为CheckoutViewController 中的self

cell.boolDelegate = self

【讨论】:

【解决方案2】:

你需要在CheckoutViewControllercellForRowAtIndexPath方法中设置委托类

cell. boolDelegate = self 

【讨论】:

  • 仅供参考,它是tableView(_:cellForRowAt:)
  • 我已经有了你在单元格中提到的代码
  • 检查 paymentRadioAction 这个函数是否调用,如果按钮被选中,你没有传递任何东西
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-19
  • 1970-01-01
相关资源
最近更新 更多