Swift:如何在每次TableView出现时重新加载新内容(例如数组)? [英] Swift: How to reload new content (eg. array) every time the tableview appears?

查看:437
本文介绍了Swift:如何在每次TableView出现时重新加载新内容(例如数组)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个视图的标签栏:

I am having a tab bar with two views:


  • tablview

  • view控制器添加数据

然后我有一个数组,该数组将通过新内容从视图控制器中更新(例如,只需追加新字符串) )。

Then I am having an array, which will be updated from the view controller with new content (eg. just append new strings).

此数组填充表格视图。
现在,当应用程序启动时,表格视图显示了所有数组的内容,但是当我通过选项卡栏移动到视图控制器,将内容添加到数组并通过选项卡栏移动回到表格视图时,我无法更新表视图,以便从数组中提取新内容。

This array populates the table view. Now when the app starts the table view shows all the arrays content, but when I move via the tab bar to the view controller, add content to the array and move back via tab bar to the tableview, I am not able to update the tableview so that it pulls the new content from the array.

我尝试使用 tableview.reloadData() viewDidAppear viewDidLoad 中的c>,但是tableview不会重新加载,仍然显示它最初加载的数据在应用启动时,但不是新内容。

I tried to use tableview.reloadData() in both viewDidAppear and viewDidLoad, but the tableview doesn't reload and still shows the data it has initially loaded at the app start, but not the new content.

override func viewWillAppear(_ animated: Bool) {
        tableView.reloadData()
        weightArray = UserDefaults.standard.array(forKey: "favorites") as! [String]
    }

这是我填充表格视图的方式:

This is how I populate the tableview:

// MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return weightArray.count
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifier = "FavoriteCell"
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)

        let weights = weightArray[indexPath.row]
        cell.textLabel?.text = weights

        return cell
    }


推荐答案

您的 reloadData()调用是在更新数组之前。

Your reloadData() call is before updating array.

应该在更新数据源之后:

It should be after updating the datasource:

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

    weightArray = UserDefaults.standard.array(forKey: "favorites") as! [String]
    tableView.reloadData()    
}






无论如何,您都可以使用数据源对象的 didSet 属性观察器。

//your weightArray declaration
var weightArray = [String]() {
    didSet {
        tableView.reloadData()
    }
}

然后您可以执行以下操作:

and then you can do:

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

    weightArray = UserDefaults.standard.array(forKey: "favorites") as! [String]   
}

这样,无论何时更改数组,您的 tableView 将重新加载。

This way, anytime you change the array, your tableView will reload.

这篇关于Swift:如何在每次TableView出现时重新加载新内容(例如数组)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆