Core Data by Tutorials by By Pietro Rea & By Aaron Douglas & Matthew Morey

Core Data by Tutorials by By Pietro Rea & By Aaron Douglas & Matthew Morey

Author:By Pietro Rea & By Aaron Douglas & Matthew Morey
Language: eng
Format: epub
Publisher: Ray Wenderlich


Great! It seems like most things worked, but there are two problems. First, the console is warning you that the table view is laying out its cells before it's on screen, and the second is that the teams seem to be grouped by qualifying zone but the section headers are gone.

The console warning is happening because things are happening in a different order now. When the view controller was the data source of the table, and you were implementing the old fetched results controller delegate methods, then the table wasn't asking for any information until it was loaded and added to the screen. Now you're using a diffable data source, and the first change happens when you call performFetch() on the results controller, which in turn calls controller(_: didChangeContentWith:), which "adds" in all of the rows from the first fetch. You call performFetch() in viewDidLoad(), which happens before the view is added to the window. Phew!

To fix this, you need to perform the first fetch later on. Remove the do / catch statement from viewDidLoad(), since that's now happening too early in the lifecycle. Implement viewDidAppear(_:), which is called after the view is added to the window:

override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) UIView.performWithoutAnimation { do { try fetchedResultsController.performFetch() } catch let error as NSError { print("Fetching error: \(error), \(error.userInfo)") } } }

Build and run, and the console warning is gone. Now to fix the section headers.

Why have they disappeared? Earlier, when you removed the implementation of UITableViewDataSource, you also removed tableView(_:titleForHeaderInSection:). This method provided the strings to populate the section headers, and without those strings the headers disappeared.

There is no way to turn these headers back on with UITableViewDiffableDataSource so you'll take an alternate route. Find the section that implements UITableViewDelegate methods and implement these two:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let sectionInfo = fetchedResultsController.sections?[section] let titleLabel = UILabel() titleLabel.backgroundColor = .white titleLabel.text = sectionInfo?.name return titleLabel } func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 20 }

Instead of just returning the title the populate the section headers, these two delegate methods create and return the UILabel to display along with the height of the section header.

Build and run to see if that brought back the missing headers:



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Popular ebooks
Salesforce Platform App Builder Certification Guide by Paul Goodey(1889)
Salesforce Advanced Administrator Certification Guide by Enrico Murru(1447)
Microsoft Power Platform Functional Consultant: PL-200 Exam Guide by Julian Sharp(1257)
Implementing Microsoft SharePoint 2019 by Lewin Wanzer and Angel Wood(1225)
Office 365 User Guide by Nikkia Carter(1160)
Scrivener for Dummies by Gwen Hernandez(565)
Advanced Excel Success by Alan Murray(541)
Automated Data Analysis Using Excel by Bissett Brian D.;(538)
Personal Finance in Your 20s & 30s For Dummies by Eric Tyson(513)
EXCEL 2021: Learn Excel Essentials Skill with Practical Exercises for Dummies by STRATVERT KEVIN(493)
Excel Dashboards and Reports for Dummies by Michael Alexander(485)
Excel 2019 All-In-One for Dummies by Harvey Greg;(478)
Basic SPSS Tutorial by Manfred te Grotenhuis & Anneke Matthijssen(470)
Tableau Desktop 10: Get up and running in a blaze with visual modular examples! by Jaxily(466)
Excel Bible for Beginners: Excel for Dummies Book Containing the Most Awesome Ready to use Excel VBA Macros by Suman Harjit(457)
Dashboarding and Reporting with Power Pivot and Excel: How to Design and Create a Financial Dashboard with PowerPivot – End to End by Kasper de Jonge(451)
Dashboarding and Reporting with Power Pivot and Excel by de Jonge Kasper(449)
Microsoft Office Access 2007 Step by Step by Steve Lambert & M. Lambert & Joan Lambert(447)
Excel 2007 Dashboards & Reports For Dummies by Michael Alexander(403)
Excel Bible for Beginners: Excel for Dummies Guide to the Best Excel Tools, Tips and Shortcuts you Must Know by Suman Harjit(402)