3

I have a list of accounts in a TableView. After I press a button, an item is deleted. So far so good.

How to refresh tableView after deletion? Please find the below screenshot for more information.

TableView is in another ViewController, the button to delete is in a ViewControllerCell

enter image description here

class cellAccount: UITableViewCell {

    @IBOutlet weak var imgAccount: UIImageView!
    @IBOutlet weak var lblNomeAccout: UILabel!
    @IBOutlet weak var btnDeletar: UIButton!
    @IBAction func btnDeletar(sender: AnyObject) {
        print(btnDeletar.titleLabel?.text)
        if (bdAccount.indexOf((btnDeletar.titleLabel?.text)!) != nil) {
            print(bdAccount.indexOf((btnDeletar.titleLabel?.text)!))
            bdAccount.removeAtIndex(bdAccount.indexOf((btnDeletar.titleLabel?.text)!)!)
            bdAccount.sortInPlace()


            self.tableView.reloadData()  // How to Reload???



        }
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
Insane Skull
  • 9,220
  • 9
  • 44
  • 63
Muricy
  • 113
  • 2
  • 9
  • 1
    reload in your ViewController.. not in tableviewcell – Lee Mar 29 '16 at 09:20
  • The correct practice i think is create delegate or a block then handle the button touch event in your ViewController, not inside TableViewCell – Tj3n Mar 29 '16 at 10:14

3 Answers3

2

You can try this way, add a NSNotificationCenter in your ViewController viewDidLoad

NSNotificationCenter.defaultCenter().addObserver(self, selector: "reloadData:",name:"reloadData", object: nil)

And then a its selector, means function

func reloadData(notification:NSNotification){
    // reload function here, so when called it will reload the tableView
    self.TableView.reloadData()
}

After both the above had been added in your viewController, now you need to call/fire this notification to reload your TableView. So inside your btnDelete clicked,

@IBAction func btnDeletar(sender: AnyObject) {
    print(btnDeletar.titleLabel?.text)
    if (bdAccount.indexOf((btnDeletar.titleLabel?.text)!) != nil) {
        print(bdAccount.indexOf((btnDeletar.titleLabel?.text)!))
        bdAccount.removeAtIndex(bdAccount.indexOf((btnDeletar.titleLabel?.text)!)!)
        bdAccount.sortInPlace()


        // This will fire the Notification in your view controller and do the reload.
        NSNotificationCenter.defaultCenter().postNotificationName("reloadData",object: self)

    }
}
Lee
  • 850
  • 11
  • 23
  • Thaks Work Fine NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewControllerAccout.reloaddData(_:)), name: "reloaddData", object: nil) – Muricy Mar 29 '16 at 14:53
0

If your tableView uses the bdAccount array as input for the amount of sections, row and the data for cellForRowAtIndexPath than it is just that.

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return bdAccount.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)

    // Configure the cell...
    cell.textlabel?.text = bdAccount.labelText // or something like that

    return cell
}

If you now call the tableView.reloadData() method it will reload the entire tableView and since it is based on your array where you just deleted one entity, that entity will also have disappeared from the tableView.

Emptyless
  • 2,964
  • 3
  • 20
  • 30
  • Thank you for your feedback, but my TableView is in another ViewController . The button to delete is in a ViewControllerCell – Muricy Mar 29 '16 at 09:27
  • Than take a look at: http://stackoverflow.com/questions/20655060/get-button-click-inside-ui-table-view-cell – Emptyless Mar 29 '16 at 09:31
0

You can use

guard let index = bdAccount.indexOf(btnDeletar.titleLabel!.text) else {
    return
}

instead if (bdAccount.indexOf((btnDeletar.titleLabel?.text)!) != nil)

To remove just a row you need to store index path for cell. Add a indexPath property.

After your cell know about it's index path and table view, you have all parameters to call tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic).

Igor Bidiniuc
  • 1,540
  • 1
  • 16
  • 27