0

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.

JJHA
  • 76
  • 7
  • Depends how you have your data source for the table view set up. Might have to create a closure or a delegate to pass the updated values back or create a shared data source between the 2 cc’s. Hard to tell without seeing any code – valosip Jun 10 '22 at 06:34
  • You need to check if your data is updated or not. Also post some code so it will be easy to identify the issue. – Hammer Class Jun 10 '22 at 06:35
  • 1
    I don't see why `reloadData()` wouldn't work in `viewDidAppear`. Can you try to set a breakpoint on table view's `numberOfRowsInSection` delegate method and see if it catches it during reload? Think the problem is most likely with your datasource. – Lokesh SN Jun 10 '22 at 06:56

1 Answers1

0

It depends on presentation style also.

Refer: Swift viewWillAppear not being called after dismissing view controller

WWDC: https://developer.apple.com/videos/play/wwdc2019/224/

Rajesh Budhiraja
  • 192
  • 1
  • 12
  • In ViewWillAppear, other functions work. But only tableview.reloaddata doesn't work – JJHA Jun 10 '22 at 06:13