Я пытаюсь позволить пользователю редактировать таблицу, которая добавляет и удаляет строки.
Это работает.Но если я удаляю строку и затем добавляю, вставленная строка содержит информацию об удаленной строке.Является ли буквально удаленная строка.
Что я делаю неправильно?
@IBAction func addArticle(_ sender: UIButton) {
numItems += 1
let indexPath = IndexPath(row: numItems - 1, section: 0)
myTableView.beginUpdates()
myTableView.insertRows(at: [indexPath], with: .automatic)
myTableView.endUpdates()
view.endEditing(true)
}
@objc func removeArticle(sender: UIButton) {
numItems -= 1
let indexPath = IndexPath(row: sender.tag, section: 0)
myTableView.beginUpdates()
myTableView.deleteRows(at: [indexPath], with: .automatic)
myTableView.endUpdates()
}
Источник данных и делегат для просмотра таблицы следующим образом:
extension MyViewController: UITableViewDataSource, UITableViewDelegate{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return numItems
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
numItems -= 1
self.myTableView.beginUpdates()
self.myTableView.deleteRows(at: [indexPath], with: .automatic)
self.myTableView.endUpdates()
}
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if firstUse{
firstUse = false
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! MyTableViewCell
if firstUse{
//setting cell
//...
cell.name.isEnabled = false
cell.name.textColor = UIColor.gray
cell.unit.text = //something
}
return initCell(cell: cell, indexPath: indexPath)
}
func initCell(cell: MyTableViewCell, indexPath: IndexPath) -> MyTableViewCell {
cell.name.inputAccessoryView = addDoneToolbarKeyboard()
cell.amount.inputAccessoryView = addDoneToolbarKeyboard()
cell.name.filterStrings(getFilteredBy())
cell.minusButton.tag = indexPath.row
cell.minusButton.addTarget(self, action: #selector(self.removeArticle), for: .touchUpInside)
cell.name.itemSelectionHandler = { filteredResults, itemPosition in
//irrelevant code
}
}
return cell
}
}
Заранее спасибо.