【发布时间】: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