我正在做swift 4的项目。我创造了
radiobutton
上课
tableview cell
1.我想
cell index
什么时候点击哪个
radio button
.请帮帮我。
编辑:我刚刚添加了我的全部代码。我无法解决我的问题。所有代码如下所示。我创建了一个单选按钮类,并给它一个单选按钮特性。当我尝试在下面的单元格视图中单击“获取索引”按钮时,我给出了“获取索引”按钮。dylib:以NSException error类型的未捕获异常终止。
import UIKit
class TVCIsGuvenligi: UITableViewCell {
lazy var radioButtonGroup: [RadioButton] = {
return [
btnRadioEvet,
btnRadioHayir,
]
}()
@IBOutlet var lblSoru: UILabel!
@IBOutlet var lblSoruAyrinti: UILabel!
@IBOutlet var lblEvet: UILabel!
@IBOutlet var lblHayır: UILabel!
@IBOutlet var btnRadioEvet: RadioButton!
@IBAction func btnRadioEvet(_ sender: RadioButton)
{
let indexPath = IndexPath.init(row: sender.tag, section: 0)
updateRadioButton(sender)
print("index \(indexPath)")
}
@IBOutlet var btnRadioHayir: RadioButton!
@IBAction func btnRadioHayir(_ sender: RadioButton)
{
let indexPath = IndexPath.init(row: sender.tag, section: 0)
updateRadioButton(sender)
print("index \(indexPath)")
}
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func updateRadioButton(_ sender: RadioButton){
radioButtonGroup.forEach { $0.isSelected = false }
sender.isSelected = !sender.isSelected
}
}
ViewController.swift
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellIs", for: indexPath) as! TVCIsGuvenligi
cell.lblSoru.text = myTitle[indexPath.row]
cell.lblSoruAyrinti.text = myTitle[indexPath.row]
cell.btnRadioEvet.tag = (indexPath.row)
cell.btnRadioEvet.addTarget(self, action: #selector(getter: cell.btnRadioEvet), for: UIControlEvents.touchUpInside)
print("current cell tag \(cell.btnRadioEvet.tag)")
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//getting the index path of selected row
let indexPath = tableView.indexPathForSelectedRow
//getting the current cell from the index path
let currentCell = tableView.cellForRow(at: indexPath!)! as! TVCIsGuvenligi
//getting the text of that cell.
let currentItem = currentCell.lblSoru.text
print("current item \(currentItem)")
}