【问题标题】:How to get table view index when click to radio button in swift 4?在swift 4中单击单选按钮时如何获取表格视图索引?
【发布时间】:2018-11-03 15:23:09
【问题描述】:

我正在 swift 4 中做项目。我在 tableview cell 中创建了 radiobutton 类。当点击哪个radio button 时,我想得到cell index。请帮帮我。

编辑:我刚刚添加了我的整个代码。我无法解决我的问题。所有代码如下。我创建了一个单选按钮类并给它一个单选按钮功能。我尝试将单选按钮集成到表格视图中,并在单击按钮时获取单元格的索引,如下所示,但它给出了 libc++abi.dylib: terminating with uncaught exception of type 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)")
 }

【问题讨论】:

  • 使用代理与父母沟通。
  • 我得到了一个索引,但是当我点击单选按钮时我无法得到一个索引。
  • @MeltemAkpolat,您可以轻松地在didSelectRowAt 中获取索引,但要在单元格按钮点击时获取索引,您需要将标签设置为单元格按钮。参考我的回答。
  • @Kuldeep 我试过了,但没有。 sender.tag 始终为 0。
  • @MeltemAkpolat,这行是你写的吗? currentCell.radioButton.tag = indexPath.row in cellForRow 代表

标签: ios swift uitableview radio-button


【解决方案1】:

使用,

currentCell.radioButton.tag = indexPath.row

并使用此标签获取单选按钮点击的索引

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-22
    • 1970-01-01
    • 2019-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多