Как реализовать кнопку, как на картинке ‘x’, она должна удалять ячейку, в которой находится?
’
import UIKit
class UserViewController: UIViewController {
var users: [String] = []
func addUser(nameUser: String){
users.append(nameUser)
print(nameUser)
}
func deleteUser(at index: Int){
users.remove(at: index)
tableView.deleteRows(at: [indexPath], with: .automatic)
}
func loadData(){
}
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var textField: UITextField!
@IBAction func pushAddButton(_ sender: Any) {
if textField.text != nil || textField.text == " " {
addUser(nameUser: textField.text!)
let indexPath = IndexPath(row: users.count - 1, section: 0)
tableView.beginUpdates()
tableView.insertRows(at: [indexPath], with: .automatic)
tableView.endUpdates()
textField.text = ""
view.endEditing(true)
} else {
let ac = UIAlertController(title: nil, message: "Пожалуйста введите имя игрока", preferredStyle: .alert)
let alertAction = UIAlertAction(title: "OK", style: .default, handler: nil)
ac.addAction(alertAction)
present(ac, animated: true, completion: nil)
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}
extension UserViewController: UITableViewDelegate, UITableViewDataSource{
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return users.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! UserTableViewCell
cell.textLabel?.text = users[indexPath.row]
// cell.deleteButton.addTarget(self, action: #selector(deleteUser(sender:)), for: .touchUpInside)
return cell
}
}
’