индекс кнопки в пользовательской ячейке - PullRequest
0 голосов
/ 22 мая 2019

Я создаю пользовательскую ячейку, содержащую кнопку, мне нужно создать переход от этой кнопки к другому VC, но прежде всего я бы хотел выдвинуть объект с этим переходом.

Я уже пытался использовать cell.button.tag, но мне это не удалось.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if segue.identifier == "showMap" {
            let mapVC = segue.destination as! MapViewController
            //guard let indexPath = tableView.indexPathForSelectedRow else { return }
            mapVC.place = places[] // <- "here I need index of button in cell"
        }
    }

Ответы [ 3 ]

1 голос
/ 22 мая 2019

Вместо использования segue обрабатывайте navigation программно через closure в UITableViewCell.

class CustomCell: UITableViewCell {
    var buttonTapHandler: (()->())?

    @IBAction func onTapButton(_ sender: UIButton) {
        self.buttonTapHandler?()
    }
}

В приведенном выше коде я создал buttonTapHandler - closure, который будет вызываться при каждом нажатии button внутри cell.

Теперь, в методе cellForRowAt, когда вы dequeue ячейку, установите buttonTapHandler из CustomCell.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
    cell.buttonTapHandler = {[weak self] in
        if let mapVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MapViewController") as? MapViewController {
            mapVC.place = places[indexPath.row]
            self?.navigationController?.pushViewController(mapVC, animated: true)
        }
    }
    return cell
}

В приведенном выше коде, buttonTapHandler при вызове будет push новый экземпляр MapViewController вместе с соответствующим place на основе indexPath.

0 голосов
/ 22 мая 2019

В пользовательской ячейке:

import UIKit

protocol CustomCellDelegate: class {
    func btnPressed(of cell: CustomCell?)
}

class CustomCell: UITableViewCell {

    weak var delegate: CustomCellDelegate?

    @IBAction func btnTapped(_ sender: UIButton) {
        delegate?.btnPressed(of: self)
    }

}

А в контроллере вида:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: CustomCell = tableView.dequeueReusableCell(for: indexPath)
        cell.delegate = self

        return cell
    }

    extension ViewController: CustomCellDelegate {

    func btnPressed(of cell: CustomCell?) {
        if let cell = cell, let indexPath = tableView.indexPath(for: cell) {
            // Your stuff here
        }
    }

}
0 голосов
/ 22 мая 2019

если вы не хотите выполнять свой код в методе didSelectRowAt, другой хороший подход, на мой взгляд, заключается в создании делегата вашей пользовательской ячейки. Смотрите код ниже

// This is my custom cell class
class MyCustomCell: UITableViewCell {

    // The button inside your cell
    @IBOutlet weak var actionButton: UIButton!
    var delegate: MyCustomCellDelegate?

    @IBAction func myDelegateAction(_ sender: UIButton) {
        delegate?.myCustomAction(sender: sender, index: sender.tag)
    }

    // Here you can set the tag value of the button to know
    // which button was tapped
    func configure(index: IndexPath){
       actionButton.tag = index.row
    }

}

protocol MyCustomCellDelegate {
    func myDelegateAction(sender: UIButton, index: Int)
}

Делегируйте ViewController, где вы используете свою пользовательскую ячейку.

class MyViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      let cell = tableView.dequeueReusableCell(withIdentifier: "MyCellIdentifier", for: indexPath) as! MyCustomCell

      cell.configure(index: indexPath)
      cell.delegate = self

      return cell
    }
}

И в конце настройте свой метод, расширяя свой пользовательский делегат ячейки

extension MyViewController: MyCustomCellDelegate {

    func myDelegateAction(sender: UIButton, index: Int) {
        // Do your staff here
    }
}

Надеюсь, я помог.

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