简体   繁体   中英

cellForRowAtIndexPath not being called when table view is not visible

There are a lot of questions and answers on this topic but I could not find one for this situation.

The app has been in production for about 3 years so I'm confident that the table view in question and all the other table views are already wired up correctly. I'm adding a new feature and I'm encountering something I didn't expect.

Here's the situation:

  • The user taps tab 1 displaying a list of items in a table view.
  • The user taps tab 2 and imports more items.
  • A notification is fired off.
  • The observer on the view controller on tab 1 receives the notification and invokes its selector.
  • The selector updates the model and calls reloadData: .
  • numberOfRowsInSection: gets called and returns the correct number of rows.

Up to this point everything is working exactly like I'd expect. But that's where it stops. After numberOfRowsInSection: there is nothing else. cellForRowAtIndexPath: is not called.

Obviously, the table view I'm reloading is not visible. It's on another tab.

Is this behaviour is an optimization where reloadData is simply not executed because the table isn't visible? Can anyone confirm this?

This makes perfect sense to me, except the part where your table view calls numberOfRowsInSection:. I would not expect that when the VC is not visible.

If the table view is not in the current view hierarchy it won't draw.

As Duncan points out a VC will not reload the tableView unless it is in the current view hierarchy. I've recently been working on an app which uses a lot of KVO for updating tableViews, and the best solution I could come up with was simply to mark the view as dirty by having a property BOOL needsRedraw on the viewController in question. Then when viewWillAppear: is called in that viewController you should be able to reload the tableview

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM