честно говоря, в голове полная каша(. В принципе код тут небольшой, подскажите, плиз, что надо изменить.
TVC
import UIKit
protocol NewCustomCellDelegate: class {
func didTapButton(_ cell: NewCustomCell)
}
class NewTVC: UITableViewController, NewCustomCellDelegate {
func didTapButton(_ cell: NewCustomCell) {
print("Hello!") // Работает.
let indexPath = tableView.indexPathForSelectedRow!
print(indexPath) // Выдает nil.
}
var items = [Item]()
var itemsVC = ItermsViewController()
override func viewDidLoad() {
super.viewDidLoad()
items = itemsVC.uploadData()
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? NewCustomCell
let item = items[indexPath.row]
cell?.customItemIDLabel.text = item.itemID
cell?.customCategoryLabel.text = item.category
cell?.delegate = self
didTapButton(sender: Any) // Что сюда передать в качестве sender?
return cell!
}
}
Custom Cell
import UIKit
class NewCustomCell: UITableViewCell {
@IBOutlet weak var customItemIDLabel: UILabel!
@IBOutlet weak var customCategoryLabel: UILabel!
weak var delegate: NewCustomCellDelegate?
@IBAction func didTapButton(_ sender: Any) {
delegate?.didTapButton(self)
}
}
Задача такая, что надо по нажатию кнопки, которая находится в ячейке таблицы, выполнять определенное действие. Для этого надо получить индекс ячейки и по нему вытащить значение (код действия) из массива. Частично операция уже работает, т.е. по нажатию кнопки печатается содержание принта, но не срабатывает получение индекса ячейки.