How to reload table view after deleting element in Swift 2 cell

I have a list of accounts in TableView . After I click the button, the item will be deleted. So far, so good.

How to update tableView after uninstall? See below for more details.

TableView is in another ViewController, the button for deleting is in 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 } } 
+3
ios swift swift2 tableview
source share
3 answers

You can try this way, add NSNotificationCenter to your ViewController viewDidLoad

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

And then its selector means function

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

After both of the above have been added to your viewController, now you need to call / run this notification to reload the TableView. So, inside your btnDelete pressed

 @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) } } 
+2
source share

If your tableView uses the bdAccount array as input for the number of sections, row, and data for cellForRowAtIndexPath, it's 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 reloads the entire tableView and, since it is based on your array, where you just deleted one entity, this object will also disappear from tableView.

0
source share

you can use

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

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

To remove only the row, you need to save the index path for the cell. Add the indexPath property.

After your cell finds out about this index path and table view, you have all the options to call tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) .

0
source share

All Articles