简体   繁体   中英

Swift remove data stored in UITableViewController from UITableViewCell

I have a custom class that extends UIViewController and contains a table view, with an array that stores data that populates the table view. For my custom cells, I have a button that when pressed, should remove that cell from the table view. However, I haven't been able to find a way to remove the data from the array in the table view controller and reload the data from the cell's class. Some of the answers I've seen on similar posts suggest notifications or delegation, but due to the structure of my app (tab controller) and that I already use notifications for another feature, the former is inefficient, and I don't know how to use the latter in this situation. This is the code for the custom cell class:

class CustomCell: UITableViewCell {
    @IBOutlet var label: UILabel!
    @IBOutlet var removeButton: UIButton!

    @IBAction func remove(sender: AnyObject) {
        // don't know what to put here
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
         super.init(style: style, reuseIdentifier: reuseIdentifier)
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

And here is the table view controller class:

class CustomController: UIViewController, UITableViewDataSource {
    @IBOutlet var table: UITableView!

    var data: [String] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        table.dataSource = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell: CustomCell = table.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as! CustomCell
        // put content in
        return cell
    }
}

The array data is what I want to access from remove in CustomCell . The closest I've gotten is using self.superview.superview.superview in CustomCell , which returns the view that CustomController controls. However, I have no way to get an instance of CustomController without instantiating a new one. How do I modify a variable in CustomController from the CustomCell class?

In your Custom Cell add this

class CustomCell: UITableViewCell {
 var customControllerReference: CustomController?
}

And in your Custom Controller in cellForRowAtIndexPath add this

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell: CustomCell = table.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as! CustomCell
        cell.customControllerReference = self
        return cell
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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