Как получить индекс табличного представления при нажатии на переключатель в Swift 4? - PullRequest
0 голосов
/ 24 мая 2018

Я делаю проект в Swift 4. Я создал radiobutton класс в tableview cell.Я хочу получить cell index при нажатии на которую radio button.Пожалуйста, помогите мне.

Редактировать: Я только что добавил весь свой код.Я не мог решить мою проблему.Все коды следующие.Я создал класс переключателей и дал ему функцию переключателей.Я попытался интегрировать переключатель в табличном представлении и получить индекс ячейки при нажатии кнопки, как показано ниже, но это дало libc ++ abi.dylib: завершение с неперехваченным исключением ошибки типа NSException.

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)")
 }

1 Ответ

0 голосов
/ 24 мая 2018

use,

currentCell.radioButton.tag = indexPath.row

И используйте этот тег, чтобы получить индекс по щелчку переключателя

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...