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

标签 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
    }

}

最佳答案

你可以试试这个方法,在你的ViewController viewDidLoad中添加一个NSNotificationCenter

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

然后是它的选择器,表示函数

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)

    }
}

关于ios - 如何在单元格 Swift 2 中删除项目后重新加载 tableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36279894/

相关文章:

ios - 我有两个文本字段,我需要在 IOS 中打开日期选择器和另一个时间选择器

ios - 合并视频但 AVAssetExportSession 从未完成

ios - 如何在 swift2 中的 xcode 上一段时间后关闭相机界面并打开不同的 View Controller ?

ios - 如何修复 Realm for swift 2.0 安装报错?

ios - SwiftUI Animation.fluidSpring()替换

ios - 有没有办法用 iOS 9.3 测试我的 Xcode 7.2 编译的应用程序?

ios - SwiftUI withAnimation 内部条件不起作用

ios - Firebase:iOS:Firebase 读取后数组变空

swift 问题 : About Type Casting for AnyObject

iphone - Observer/Dealloc 错误和看不见的 UIViewController 崩溃