流行视图时如何重新加载数据?

发布于 2025-02-06 04:32:53 字数 2769 浏览 0 评论 0 原文

如果我有两个viewControllers一个包含一个uitaiteView,而另一个则在表格视图中更新数据 当我弹出ViewController时,如何重新加载表数据?

我尝试了ViewWillAppear,ViewDidAppear。但这是行不通的。 恰好重新扎data工作,但没有更改表观视图,

    private var diaryList =
    [[DiaryListJsonValue.DiaryListNew.Monthly]]()
    private var expandedcells:[IndexPath] = [IndexPath]()

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        print(indexPath.section, indexPath.row)

        let isContain = expandedcells.firstIndex(of: indexPath)

        if (isContain == nil) {
            expandedcells.append(indexPath)
        }
        else {
            if let nContain = isContain {
                expandedcells.remove(at: nContain)
            }
        }

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
        if expandedcells.contains(indexPath) {

            let cell = tableView.dequeueReusableCell(withIdentifier: DiaryListCells.identifier, for: indexPath) as! DiaryListCells

            let list = diaryList[indexPath.section][indexPath.row]
            self.diaryDate = list.diaryDate
            



            return cell
         else {
            let cell = tableView.dequeueReusableCell(withIdentifier: DiaryListOffcell.identifier, for: indexPath) as! DiaryListOffcell
            let list = diaryList[indexPath.section][indexPath.row]
            
            ...
            return cell

这是TableView设置。

struct DiaryListJsonValue: Codable {
    var diaryList: [DiaryListNew]?
    
    enum CodingKeys: String, CodingKey {
        case diaryList = "DiaryList"
    }
    
    struct DiaryListNew: Codable {
        var isLast: Bool
        var monthly: [Monthly]?
        
        enum CodingKeys: String, CodingKey {
            case isLast  = "IsLast"
            case monthly = "Monthly"
        }
        
        struct Monthly: Codable {
            var diaryDate           : String // yyyy-MM-dd
            var recommendedCalorie  : Int
            var intakeCalories      : Int
            var intakeRate          : Int
            var mealIntakeCalories  : [Int?]
            
            enum CodingKeys: String, CodingKey {
                case diaryDate          = "DiaryDate"
                case recommendedCalorie = "RecommendedCalorie"
                case intakeCalories     = "IntakeCalories"
                case intakeRate         = "IntakeRate"
                case mealIntakeCalories = "MealIntakeCalories"
            }
            
        }
    }
}

这是数据结构

First ViewDidload tableview.reloddata Works,我也具有tableview.reloaddata的刷新功能。它也有效! 但是我在NextView上传的数据和PopView中执行了Sextegue NextView(数据UPLAOD视图)。我希望TableView已更新。但没有更新。

If I have two ViewControllers one which contains a UITableView and another which updates data in the tableView
How to reload table data when I pop the ViewController?

I tried ViewWillAppear, ViewDidAppear. But It is not working.
Exactly reloadData works but tableview is not changed

    private var diaryList =
    [[DiaryListJsonValue.DiaryListNew.Monthly]]()
    private var expandedcells:[IndexPath] = [IndexPath]()

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        print(indexPath.section, indexPath.row)

        let isContain = expandedcells.firstIndex(of: indexPath)

        if (isContain == nil) {
            expandedcells.append(indexPath)
        }
        else {
            if let nContain = isContain {
                expandedcells.remove(at: nContain)
            }
        }

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
        if expandedcells.contains(indexPath) {

            let cell = tableView.dequeueReusableCell(withIdentifier: DiaryListCells.identifier, for: indexPath) as! DiaryListCells

            let list = diaryList[indexPath.section][indexPath.row]
            self.diaryDate = list.diaryDate
            



            return cell
         else {
            let cell = tableView.dequeueReusableCell(withIdentifier: DiaryListOffcell.identifier, for: indexPath) as! DiaryListOffcell
            let list = diaryList[indexPath.section][indexPath.row]
            
            ...
            return cell

this is tableview setting.

struct DiaryListJsonValue: Codable {
    var diaryList: [DiaryListNew]?
    
    enum CodingKeys: String, CodingKey {
        case diaryList = "DiaryList"
    }
    
    struct DiaryListNew: Codable {
        var isLast: Bool
        var monthly: [Monthly]?
        
        enum CodingKeys: String, CodingKey {
            case isLast  = "IsLast"
            case monthly = "Monthly"
        }
        
        struct Monthly: Codable {
            var diaryDate           : String // yyyy-MM-dd
            var recommendedCalorie  : Int
            var intakeCalories      : Int
            var intakeRate          : Int
            var mealIntakeCalories  : [Int?]
            
            enum CodingKeys: String, CodingKey {
                case diaryDate          = "DiaryDate"
                case recommendedCalorie = "RecommendedCalorie"
                case intakeCalories     = "IntakeCalories"
                case intakeRate         = "IntakeRate"
                case mealIntakeCalories = "MealIntakeCalories"
            }
            
        }
    }
}

This is data struct

First viewDidLoad tableview.reloaddata works, also I have refresh function with tableview.reloaddata. It also works!
but I performsegue nextView(data uplaod view), in the nextView uploaded data and popview. I expected tableview updated. But not updated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文