可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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

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 } }
回答1:
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) } }
回答2:
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.
回答3:
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)
.