Я пытаюсь реализовать список tableView, в котором первая строка используется для добавления элементов в этот список
Для этого я создал два отдельных класса для первой ячейки и для TableView.![enter image description here](https://i.stack.imgur.com/w8CaB.png)
Класс ячеек использует протокол TextFieldDelegate для передачи введенного значения в класс TableView
protocol TextFieldDelegate {
func saveProduct (newProduct: Product)
}
class TextFieldCell: UITableViewCell {
@IBOutlet weak var insertProduct: UITextField!
@IBOutlet weak var insertQuantity: UITextField!
var delegate: TextFieldDelegate?
override func awakeFromNib() {
let vc = ListViewController()
self.delegate = vc
}
@IBAction func addButtonPressed(_ sender: UIButton) {
let newProduct = Product()
if let productText = insertProduct.text{
newProduct.name = productText
} else{
fatalError("Product name is not inserted")
}
if let quantityText = insertQuantity.text{
newProduct.quantity = Int(quantityText) ?? 0
} else{
fatalError("Product quantity is not inserted")
}
delegate?.saveProduct(newProduct: newProduct)
insertProduct.text = ""
insertQuantity.text = ""
}
}
Класс TableView соответствует указанному протоколу, сохраняетДанные в базу данных области и перезагрузка таблицы
class ListViewController: UITableViewController, TextFieldDelegate {
let realm = try! Realm()
var products: Results<Product>?
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
loadProducts()
}
//MARK: - TableView DataSource Methods
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let numberOfCell = products?.count{
guard numberOfCell == 0 else {
print("number of rows working - \(numberOfCell)")
return numberOfCell + 1 }
return 1
}
else {
return 1
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("cell for row started")
if indexPath.row == 0 {
let textCell = tableView.dequeueReusableCell(withIdentifier: "TextCell") as! TextFieldCell
return textCell
}
else {
let cell = tableView.dequeueReusableCell(withIdentifier: "ProductCell", for: indexPath) as UITableViewCell
if let item = products?[indexPath.row - 1]{
cell.textLabel?.text = item.name
cell.accessoryType = item.checked ? .checkmark : .none
}
return cell
}
}
//MARK: - TableView Delegate Methods
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("it works")
}
func loadProducts(){
products = realm.objects(Product.self)
self.tableView.reloadData()
}
//MARK: - TextFieldDelegate Method
func saveProduct(newProduct: Product){
do{
try realm.write {
realm.add(newProduct)
}
}catch{
print("Error saving context in Product \(error)")
}
tableView.reloadData()
}
@IBAction func refreshButtonPressed(_ sender: UIBarButtonItem) {
self.tableView.reloadData()
}
}
Проблема появляется после того, как я пытаюсь добавить элементы из текстовых полей.SaveProduct () сохраняет элементы в базе данных, вызывает TableView cellForRowAt и даже возвращает требуемое значение.Но после этого cellForRowAtindexPath () не вызывается.
Я попытался создать отдельную кнопку Refresh, которая просто вызывает метод данных перезагрузки, и он работает отлично.
Заранее спасибо!