如何在单元格Swift 2中删除项目后重新加载tableview [英] How to reload tableview after delete item in cell Swift 2
问题描述
我在TableView
中有一个帐户列表.当我按下一个按钮后,一个项目被删除.到目前为止,一切都很好.
I have a list of accounts in a TableView
. After I press a button, an item is deleted. So far so good.
删除后如何刷新tableView?请找到下面的屏幕截图以获取更多信息.
How to refresh tableView after deletion? Please find the below screenshot for more information.
TableView在另一个ViewController中,要删除的按钮在ViewControllerCell中
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
}
}
推荐答案
您可以尝试这种方式,在ViewController viewDidLoad
You can try this way, add a NSNotificationCenter
in your ViewController viewDidLoad
NSNotificationCenter.defaultCenter().addObserver(self, selector: "reloadData:",name:"reloadData", object: nil)
然后是它的selector
,表示功能
func reloadData(notification:NSNotification){
// reload function here, so when called it will reload the tableView
self.TableView.reloadData()
}
在您的viewController中添加了以上两项之后,现在您需要调用/触发此通知以重新加载TableView.因此,在btnDelete
中单击
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)
}
}
这篇关于如何在单元格Swift 2中删除项目后重新加载tableview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!