Please help me figure it out. There is a custom class describing the cell and there is a button. It is necessary that by clicking on the button, this cell is deleted.

import UIKit import RealmSwift class PurchesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var purchesTableView: UITableView! let manage = ManagerData() override func viewDidLoad() { super.viewDidLoad() purchesTableView.delegate = self purchesTableView.dataSource = self } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) purchesTableView.reloadData() } func numberOfSections(in tableView: UITableView) -> Int { return manage.loadPurchases().0.count } func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return manage.loadPurchases().0[section] } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return manage.loadPurchases().1[section].count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "purchesCell", for: indexPath) as! CustomPurchesTableViewCell cell.productLabel.text = manage.loadPurchases().1[indexPath.section][indexPath.row] cell.weightProductLabel.text = manage.loadPurchases().2[indexPath.section][indexPath.row] cell.weightNameLabel.text = manage.loadPurchases().3[indexPath.section][indexPath.row] // cell.boughtButton.addTarget(self, action: #selector(removeProduct), for: .touchUpInside) return cell } } class CustomPurchesTableViewCell: UITableViewCell { @IBOutlet weak var boughtButton: UIButton! @IBOutlet weak var productLabel: UILabel! @IBOutlet weak var weightProductLabel: UILabel! @IBOutlet weak var weightNameLabel: UILabel! @IBAction func removePurches(_ sender: Any) { print("remove") } } 

data acquisition method

 func loadPurchases() -> ([String], Array<Array<String>>, Array<Array<String>>, Array<Array<String>>) { var sections: [String] = [] var product = Array<Array<String>>() var weight = Array<Array<String>>() var nameWeight = Array<Array<String>>() let realm = try! Realm() let data = realm.objects(Purches.self) for item in data { if sections.contains(item.nameDish) == false { sections.append(item.nameDish) } } for a in sections { var productArr = Array<String>() var weightArr = Array<String>() var nameWeightArr = Array<String>() for prod in data { if a == prod.nameDish { productArr.append(prod.product) weightArr.append(prod.weight) nameWeightArr.append(prod.nameWeigh) } } product.append(productArr) weight.append(weightArr) nameWeight.append(nameWeightArr) } return (sections, product, weight, nameWeight) } 

    1 answer 1

    The implementation of lists and tables in applications can be difficult and confusing, but the idea is generally simple: you have a table controller that has an array of data (or several, you have 3 items) and cells that are dynamically created for each value of the array, and also twist , the table itself. A full answer in the form of a code to paint for a long time, and the Internet is full of full-fledged projects, so I will try to give explanatory tips.

    For the mechanics described by you, the following is necessary: ​​each cell must know its index in the array and have a reference to the controller object. When a button is pressed, the cell refers to the controller by reference and sends its index so that it deletes it and updates the table. Both that and another need to be transferred in the designer at cell creation in the [ tableView cellForRowAt ] method.

    That is, in the controller you should have a method like delete_row_at_index:(int)ind , there you delete the corresponding array indices and call the reloadData method of your object purchesTableView . After that, he himself will turn to the controller object, he will indicate the new number of rows (after all, the length of the array has been changed) and then will output the necessary cells.

    Other:

    • I didn’t quite understand if you used sections. If not, I advise you to put return 1 in the numberOfSections method. And if so, then think - perhaps all of them are easier to implement in a single cell. To get rid of constructions of the kind manage.loadPurchases().1[section]

    • Three arrays ( purcheses , countWeight , countNameWeight ) ideally should be replaced with one to use a dictionary or custom object. Changing the size of one array is easier than just three.

    • Thank! In the controller itself, I do not use arrays. I use the sections, this will not fail because The section name is a divisor of the entire list. Added another method with which I get the data to fill. The data itself is stored in REALM - Egor
    • @Egor Then, probably, you need to transfer the ID position from the database to the cells. And look, maybe the number of queries to the database should be reduced and keep them in variables so as not to carry out this large method all the time. And gratitude on this site is expressed by the up arrow and check of the accepted answer ;) - AivanF.
    • ID is not and is not planned, but he does not keep his ID. To delete, in any case, I need 2 values ​​(the section name and the name from the cell) - Egor