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

时间:2016-03-29 09:07:31

标签: ios swift swift2 tableview tableviewcell

我在TableView中有一个帐户列表。按下按钮后,项目将被删除。到现在为止还挺好。

删除后如何刷新tableView?有关详细信息,请参阅以下屏幕截图。

TableView位于另一个ViewController中,要删除的按钮位于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 个答案:

答案 0 :(得分:2)

您可以尝试这种方式,在ViewController中添加NSNotificationCenter 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点击内,

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

    }
}

答案 1 :(得分:0)

如果你的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中消失。

答案 2 :(得分:0)

您可以使用

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

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

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

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