ios 如何使用视图控制器 (swift) 重新加载 tableview

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33401216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:08:01  来源:igfitidea点击:

How to reload tableview using view controller (swift)

iosswiftuitableviewreloaddata

提问by Netzer

i'm quite new to this. I've spent some hours to go through the various questions on this topic but couldn't find an answer that fits to me question.

我对此很陌生。我花了几个小时来浏览关于这个主题的各种问题,但找不到适合我问题的答案。

I have an viewcontroller (not a tableviewcontroller) with a tableview as subview. My question is how to reload the table data inside the :viewwillappear method of the view controller. Inside the method i can't "access" the tableview instance. (Also i understand i can't use reloaddata() because im not using a tableviewcontroller).

我有一个视图控制器(不是 tableviewcontroller),它有一个 tableview 作为子视图。我的问题是如何在视图控制器的 :viewwillappear 方法中重新加载表数据。在方法内部,我无法“访问” tableview 实例。(我也知道我不能使用 reloaddata() 因为我没有使用 tableviewcontroller)。

Do you know any simple solution? (i very much assume that a protocol could help here?)

你知道任何简单的解决方案吗?(我非常假设协议可以在这里提供帮助?)

this is the code in short:

这是简短的代码:

class ViewController3: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var Person_data = [Person]()

    override func viewDidLoad() {
        super.viewDidLoad()

        let tb: UITableView = UITableView(frame: CGRect(x: 10, y: 50, width: 200, height:600), style: UITableViewStyle.Plain)

        tb.dataSource = self
        tb.delegate = self
        super.view.addSubview(tb)

        fetch_data()
    }


        func tableView(tableview: UITableView, numberOfRowsInSection section: Int) -> Int {
            let rows = Person_data.count
            return rows
        }


       func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
        {  
            let cell: UITableViewCell = UITableViewCell()
            cell.textLabel?.text = Person_data[indexPath.row].name
            return cell
        }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        fetch_data()      
      // reload at this point

    } 


    func fetch_data() {

       let tb_fetchRequest = NSFetchRequest(entityName: "Person")
        do {

            let tb_fetchResults = try my_moc.executeFetchRequest(tb_fetchRequest) as? [Person]
            Person_data = tb_fetchResults!
        } catch let error as NSError {
                print("request error: \(error)")
            }

        }
    }

采纳答案by Paul Cantrell

Your problem is that tbis a local variable, so it is not visible outside of viewDidLoad().

你的问题是这tb是一个局部变量,所以它在viewDidLoad().

If you make tba property (a.k.a. instance variable) of your view controller, then you can use it from any of the controller's methods:

如果您创建tb视图控制器的属性(又名实例变量),那么您可以从控制器的任何方法中使用它:

class ViewController3: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var Person_data = [Person]()

    private var tb: UITableView?  // Now the scope of tb is all of ViewController3...

    override func viewDidLoad() {
        super.viewDidLoad()

        // ...so it is visible here...

        tb = UITableView(frame: CGRect(x: 10, y: 50, width: 200, height:600), style: UITableViewStyle.Plain)

        ...
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        fetch_data()      

        tb?.reloadData()   // ...and it is also visible here.
    } 

Note that in your code, the fact that tbis a local variable doesn't mean that your UITableViewobject disappears when viewDidLoad()finishes. It means that your referenceto the UITableViewdisappears. The table view object itself lives on because it is added to your view hierarchy, but you don't have a reference to it anymore because your variable went out of scope.

请注意,在您的代码中,tb作为局部变量的事实并不意味着您的UITableView对象在viewDidLoad()完成时消失。这意味着你参考UITableView消失。表视图对象本身仍然存在,因为它已添加到您的视图层次结构中,但您不再引用它,因为您的变量超出了范围。

For further reading on the distinction between variables and the objects they reference, search for “scope” and “pass by reference.”

要进一步了解变量与其引用的对象之间的区别,请搜索“范围”和“通过引用传递”。

回答by Charles A.

You can access your tableViewIBOutletin viewWillAppear:, you can also call reloadData. I'm not sure why you think you can't, but it should work fine.

你可以访问你的tableViewIBOutletin viewWillAppear:,你也可以调用reloadData。我不确定为什么你认为你不能,但它应该可以正常工作。

回答by Arsen

You have to create a variable with link to tableView and create it before viewWillAppear.

您必须创建一个带有 tableView 链接的变量,并在 viewWillAppear 之前创建它。

class ViewController3: UIViewController, UITableViewDataSource, UITableViewDelegate {
    private let tableView = UITableView(frame: CGRect(x: 10, y: 50, width: 200, height:600), style: UITableViewStyle.Plain)
    var Person_data = [Person]()

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.dataSource = self
        tableView.delegate = self
        super.view.addSubview(tb)

        fetch_data()
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        fetch_data()      
        tableView.reloadData()
    } 
}

回答by Arsen

class ViewController3: UIViewController, UITableViewDataSource, UITableViewDelegate {
    private let tableView = UITableView(frame: CGRect(x: 10, y: 50, width: 200, height:600), style: UITableViewStyle.Plain)
    var Person_data = [Person]()

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.dataSource = self
        tableView.delegate = self
        super.view.addSubview(tb)

        fetch_data()
    }



    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        fetch_data()      
        tableView.reloadData()

    } 
    }