简体   繁体   English

删除特定部分中的UITableView单元格

[英]Deleting a UITableView cell in a specific section

There is a task. 有一个任务。 Each cell contains a button by clicking which you want to delete this cell. 每个单元格都包含一个按钮,通过单击您要删除的单元格。 The problem is that sections are used to delineate the entire list by category. 问题在于,各节用于按类别描绘整个列表。 The data I take from Realm DB. 我从Realm DB获取的数据。 removal must occur under two conditions because the name is repeated, so you need to consider the name from the label and the name of the section. 由于名称是重复的,因此必须在两种情况下进行删除,因此您需要考虑标签中的名称和节的名称。 I will be very grateful for the sample code with comments. 我将非常感谢带有注释的示例代码。

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")
    }
}

method for get data 获取数据的方法

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)
    }

Index path you will get in cell class Index path have two property section and row for table view 您将在单元格类中获得的索引路径索引路径具有两个属性部分和用于表视图的行

Now you can create on more method in Controller class and assign to a variable to every cell or you can use editAction provided by table view for delete 现在,您可以在Controller类中创建更多方法,并将其分配给每个单元格的变量,或者可以使用表视图提供的editAction进行删除

in order to get number section and row you need create IBOutlet in custom cell and on ViewController class is created addTarget for your button. 为了获取数字部分和行,您需要在自定义单元格中创建IBOutlet,并在ViewController类上为按钮创建addTarget。 Example code at the bottom. 底部的示例代码。

import UIKit
import RealmSwift

class PurchesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var purchesTableView: UITableView!
let manage = ManagerData()

          //... more code ...

    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(removePurches(_:)), for: .touchUpInside)

        return cell

    }

    @objc func removePurches(_ sender: UIButton) {
        let position: CGPoint = sender.convert(CGPoint.zero, to: purchesTableView)
        let indexPath: IndexPath! = self.purchesTableView.indexPathForRow(at: position)
        print("indexPath.row is = \(indexPath.row) && indexPath.section is = \(indexPath.section)")
        purchesTableView.deleteRows(at: [indexPath], with: .fade)
    }
}

and custom class CustomPurchesTableViewCell for cell 和单元格的自定义类CustomPurchesTableViewCell

class CustomPurchesTableViewCell: UITableViewCell {
    @IBOutlet weak var boughtButton: UIButton! // you button for press
    @IBOutlet weak var productLabel: UILabel!
    @IBOutlet weak var weightProductLabel: UILabel!
    @IBOutlet weak var weightNameLabel: UILabel!
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM