Swift 4 UITableView Like Apple Music Search - ios

I am using UITableViewController and I am playing an having multiple sections to my UITableViewController, I am just curious how do I get headings like Apple Music Search, see screenshot below:
I am talking about the 'Recent' and 'Trending' headings, are those just the section headings or multiple tables?

Use built-in iOS table view section headers. Place your titles in the collection/ array and display it as follows:
let titles = ["Section 1", "Section 2", "Section 3"]
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return titles[Int]
}
Also, you need to define number of sections:
func numberOfSections(in tableView: UITableView) -> Int {
return titles.count
}

Related

Why headerView is always nil in numberOfRowsInSection?

I have to make a dynamic datasource for a table, that satisfies the criteria:
we do not know exact number of sections
we do not know exact title for header in section
we do not now how many rows it will be in each section each time
To detect number of rows per section I have the following code:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let header = tableView.headerView(forSection: section)
return self.servicesData?.filter({ $0.clubName == header?.textLabel?.text }).count ?? 0
}
where servicesData is the object array that is to be filtered by the header title aka clubName. Titles are gathered from the object array.
For some reason I always get nil when try to access the header. So far I tried calling tableView(_:titleForHeaderInSection:) directly in numberOfRowsInSection but obviously with no luck.
Feedback much appreciated.
It is nil because it is not created at that moment. You should not use tableView.headerView(forSection: section) in numberOfRows. You should use the model from where you get the title to use it to filter in the array.
You should fetch all of your data before populating the table view and first. Then you should implement:
func numberOfSections(in tableView: UITableView) -> Int {
return headerModels.count
}
where headerModels it's an array of objects with your title and it's items like:
class HeaderModel {
let title: String
let items: [YourItem]
}
and then:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return headerModels[section].items.count
}
So, as per #shurtugal 's solution it is possible to pass-in an array of objects to populate the table. I thought that creating a utility class for this very case may be too much. So here's the slightly altered way:
Consider a dictionary var servicesData = [String: [CustomObject]]() that is populated each time the ViewController shows up. The dictionary can have indefinite number of keys and indefinite number of values inside each key. Algorithm of populating the dictionary is taken from this answer.
Thereby, UITableViewDatasource methods can be defined like this:
func numberOfSections(in tableView: UITableView) -> Int {
self.servicesData.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Array(self.servicesData.values)[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
/* cell creation code code */
/// sample line
cell?.someLabel.text = Array(self.servicesData.values)[indexPath.section][indexPath.row]
return cell
}

Sync tabs with tableview scroll in IOS

I am working on a food delivery app where in a single store i have multiple menu categories. I have items in single tableview. So when i reach the header ( for example Breakfast then Breakfast tab should be active ) of each section how to change tab accordingly?enter image description here
use this two method to put header in section..
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Section \(section)"
}
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let vw = UIView()
vw.backgroundColor = UIColor.red
return vw
}

Swift: Table view section titles

Can any one explain the difference between these two section titles and that how can I achieve the default iOS section titles as in the setting image shown below. Thanks for the help.
Im using the following function to set title and I expect this to be a default feature.
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "This is me title"
}
This is the title I get on my sections.
The same header as Settings can be achieved by using tableView's Grouped style

TitleForHeaderInSection not displaying correctly

This app is just for research and learning purposes. I'm new to developing, so there is nothing fancy about that, really.
It should display a TableView with some contacts, which are split into two different categories: Recent & Friends
However, when I try to return those to be displayed as a header (everything else worked just as I intended it to) with the TitleForHeaderInSection function, only one gets displayed eventually. (I used a switch function, so for that matter only case 0 is displaying).
override func numberOfSections(in tableView: UITableView) -> Int {
return 2 // Recent & Friends
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
switch section {
case 0: return "RECENT"
case 1: return "FRIENDS"
default: break
}
return String(section)
}
As seen in the screenshot above, in the app only one of the two cases gets returned and I am genuinely struggling to figure out why.
is it showing sections or sections are missing, not just heading ?if sections are missing then you need to select tableview style grouped in storyboard
Its because you dint implement these delegates :(1) viewforHeaderInSection (2)heightForHeaderInSection
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
//Create label which you want to show on each section header and return it
}
Also use heightForHeaderInsection method .
Hope this help to solve your issue

How to edit multiple titles of sections in UITableViews?

My UITableView contains multiple sections and I would like these sections to have different names. The titleForHeaderInSection method is provided to change the name of a section. It returns a String and it looks like this:
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "My First Section"
}
This is great for changing one name, but what if I have multiple sections? Should I use more than one functions? Should I declare the names in some ways in the viewDidLoad or numberOfSectionsInTableView methods? They don't like the right choices, though.
How to edit multiple titles of sections in UITableViews?
Using an array of your section titles, return the correct string like so:
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let sectionInfo = arrayOfSectionNames[section]
return sectionInfo
}

Resources