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

时间:2015-10-28 20:40:42

标签: ios swift uitableview reloaddata

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

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

你知道任何简单的解决方案吗? (我非常假设一个协议可以帮助吗?)

这是简短的代码:

0

5 个答案:

答案 0 :(得分:5)

您的问题是tb是一个局部变量,因此在viewDidLoad()之外不可见。

如果你使tb视图控制器的属性(a.k.a。实例变量),那么你可以从任何控制器的方法中使用它:

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.
    } 

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

要进一步阅读变量与它们引用的对象之间的区别,请搜索“范围”和“按引用传递”。

答案 1 :(得分:1)

您可以访问tableView中的IBOutlet viewWillAppear:,也可以致电reloadData。我不确定你为什么认为你不能,但它应该可以正常工作。

答案 2 :(得分:1)

您必须创建一个带有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()
    } 
}

答案 3 :(得分:0)

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

    } 
    }

答案 4 :(得分:0)

快速重新加载tableView

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

   //This is outlet of tableView...
 @IBOutlet public var userTableView : UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        userTableView.delegate = self
        userTableView .datasource = self
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        fetchData()      
     
    }

 func fetchData(){
       //Here you can fetch your required data

       //Here the tableView will be reload...
        userTableView.reloadData()

 }
相关问题