如何在单元格 Swift 2 中删除项目后重新加载 tableview

How to reload tableview after delete item in cell Swift 2

我在 TableView 中有一个帐户列表。按下按钮后,一个项目被删除。到目前为止,一切都很好。

tableView删除后如何刷新?请查看以下屏幕截图以获取更多信息。

TableView 在另一个 ViewController 中,要删除的按钮在 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
    }

}

如果您的 tableView 使用 bdAccount 数组作为部分、行和 cellForRowAtIndexPath 的数据的输入,那么它就是这样。

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
}

如果您现在调用 tableView.reloadData() 方法,它将重新加载整个 tableView,并且由于它基于您刚刚删除一个实体的数组,该实体也将从 tableView 中消失。

你可以这样试试,在你的ViewControllerviewDidLoad

中加一个NSNotificationCenter
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 中添加以上两项后,现在您需要 call/fire 此通知以重新加载您的 TableView。所以在你的 btnDelete 里面点击了,

@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)

    }
}

您可以使用

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

而不是 if (bdAccount.indexOf((btnDeletar.titleLabel?.text)!) != nil)

要仅删除一行,您需要存储单元格的索引路径。添加一个indexPath属性。

在您的单元格知道它的索引路径和 table 视图后,您就拥有了调用 tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) 的所有参数。