我正在尝试实现BEMCheckBox,而myCheckBox.delegate = self
必须位于viewDidLoad
中。
如何在tableView
中执行此操作?
我已经对单元格和tableView完成了此操作:
tableView
的单元格文件:
import UIKit
import BEMCheckBox
protocol SizeSelectionDelegate: NSObjectProtocol {
func didChooseSmall(cell: SizeSelectorCell)
func didChooseLarge(cell: SizeSelectorCell)
}
class SizeSelectorCell: UITableViewCell {
@IBOutlet weak var smallSizeCheckBox: BEMCheckBox!
@IBOutlet weak var largeSizeCheckBox: BEMCheckBox!
@IBOutlet weak var smallSizePriceLabel: UILabel!
@IBOutlet weak var largeSizePriceLabel: UILabel!
weak var delegate: SizeSelectionDelegate?
func didTap(_ checkBox: BEMCheckBox) {
if checkBox.tag == 0 {
delegate?.didChooseSmall(cell: self)
}
if checkBox.tag == 1 {
delegate?.didChooseLarge(cell:self)
}
}
}
TableViewController
:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "InfoCell") as! InfoTableViewCell
cell.nameLabel.text = name
cell.detailLabel.text = detail
cell.smallPriceLabel.text = String (smallPrice)
cell.largePriceLabel.text = String (largePrice)
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "SizeSelector") as! SizeSelectorCell
cell.smallSizePriceLabel.text = String (smallPrice)
cell.largeSizePriceLabel.text = String (largePrice)
return cell
}
}
extension InfoTableViewController: SizeSelectionDelegate
{
func didChooseSmall(cell: SizeSelectorCell) {
size = "Small"
print(size)
}
func didChooseLarge(cell: SizeSelectorCell) {
size = "Large"
print(size)
}
}
答案 0 :(得分:2)
您需要在cell.delegate = self
内设置委托人cellForRowAt
let cell = tableView.dequeueReusableCell(withIdentifier: "SizeSelector") as! SizeSelectorCell
cell.delegate = self
cell.smallSizePriceLabel.text = String (smallPrice)
cell.largeSizePriceLabel.text = String (largePrice)
return cell