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

How to reload tableview using view controller (swift)

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

我有一个 viewcontroller(不是 tableviewcontroller),其中一个 table 视图作为子视图。我的问题是如何在视图控制器的 :viewwillappear 方法中重新加载 table 数据。在方法内部,我无法 "access" table 查看实例。 (我也知道我不能使用 reloaddata() 因为我没有使用 tableviewcontroller)。

你知道什么简单的解决办法吗? (我非常认为协议可以帮助解决这个问题?)

这是简短的代码:

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)")
            }

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

    } 
    }

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

你的问题是 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() 完成时消失。这意味着您对 UITableViewreference 消失了。 table 视图对象本身存在,因为它已添加到您的视图层次结构中,但您不再有对它的引用,因为您的变量超出了范围。

要进一步了解变量及其引用的对象之间的区别,请搜索“作用域”和“按引用传递”。

您必须为 tableView 创建一个带有 link 的变量,并在 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()
    } 
}