0

I have a food ordering app that has an "order" tab with a table showing the items on the order and below the table, labels with the $ sub total and total. The order data is stored in a SQLite database.

I have a function named loadFromSQL which reads the data into arrays that are used to populate the table cells and $ total labels.

I call loadFromSQL from viewDidLoad, viewDidAppear and an @IBAction func didSwipeDown. The table is refreshed when run from viewDidLoad and the swipe-down but not from viewDidAppear. I know the SQL is being read because the $ total labels are being updated even when called from viewDidAppear.

I call self.tableView.reloadData in DispatchQueue.main.async right after updating the $ total labels at the end of loadFromSQL.

Here are some code snippets:

@IBAction func didSwipeDown() {
    loadFromSQL()
}


override func viewDidLoad() {
    super.viewDidLoad()

    loadFromSQL()

    let nib = UINib(nibName: "OrderTableViewCell", bundle: nil)
    tableView.register(nib, forCellReuseIdentifier: "OrderTableViewCell")
    tableView.delegate = self
    tableView.dataSource = self
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    loadFromSQL()
}

This is from the end of loadFromSQL:

sqlite3_finalize(selectStatementQuery)
//calculate sub total and total
dispOrderTotal = dispTotTax + dispSubTotal
let formattedTax = String(format: "$%.2f", dispTotTax)
let formattedSub = String(format: "$%.2f", dispSubTotal)
let formattedTotal = String(format: "$%.2f", dispOrderTotal)
self.orderTotalLab.text = formattedTotal
self.taxLab.text = formattedTax
self.subTotLab.text = formattedSub

DispatchQueue.main.async {
    self.tableView.reloadData()
}

Any ideas would be greatly appreciated.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
stvely
  • 11
  • 1
  • 1
    Confirm that `viewDidAppear` is getting called when you think it should be. There are many scenarios where it isn’t called when one otherwise might expect it to be (e.g., it isn't called when the app comes back into foreground, after presenting a non-full screen modal, etc.), but without information about what you’re doing that you expected `viewDidAppear` to be called, it’s impossible to diagnose. – Rob Dec 13 '21 at 20:38
  • 1
    Unrelated, but in `viewDidLoad`, I might advise calling `loadFromSQL` after you finish configuring the table view, not before. Because you’re dispatching the `reloadData` back to the main queue, it doesn't matter, but one generally wouldn't want to bury that sort of dependency inside `loadFromSQL`, and there's no downside to finish configuring the table view before attempting to populate it. – Rob Dec 13 '21 at 20:39
  • Thanks for the response. Per your suggestion, I moved the call to loadFromSQL to the end of viewDidLoad. Regarding your first comment, I know loadFromSQL is being called from viewDidAppear because I had logging in viewDidAppear and the $ totals labels – stvely Dec 13 '21 at 20:56
  • OK. But the bottom line is that you can (and many of us do) reload tables in `viewDidAppear`, so the problem rests elsewhere. – Rob Dec 13 '21 at 21:05
  • I’m sure it’s something I’m doing or not doing I’m just trying to figure out what it is. The comments cut me off before I could address your first comment. My app has tabs, two of which are “Menu” and “Order” the user selects items off of the “Menu” tab and then reviews the selected items on the “Order” tab. The first time you go to the order tab all selected items are there. If you go back to “Menu” and select another item and then back to order the new item does not show but is reflected in the $ totals. I think I’ll take a second look at my SQL query. – stvely Dec 13 '21 at 21:22

1 Answers1

0

Ok, I found the problem. I needed the table height to be dynamic so in the "cellForRowAt" func I was using cell.bounds.height to set the heightConstraint.constant. However early on I was getting bogus cells and I noticed they had the default height of 79.0 so I filtered the bogus cells using that. At some point I fixed what ever was causing the bogus cells but left the filtering in. What was causing the problem I asked about above was for some reason viewDidApear returns the default cell height of 79.0 for new items. causing the table to be too small. So the new items were in the table I just didn't scroll down to see them. I was able to fix the problem with help from this answer.

I added this to my cellForRowAt:

    setHeight = self.tableView.contentSize.height
    if setHeight > maxHeight {
        setHeight = maxHeight
    }
    self.heightConstraint.constant = self.setHeight

I set maxHeight based on the screenSize.height so I can keep it the size I want across different devices.

stvely
  • 11
  • 1