How to edit multiple titles of sections in UITableViews? - ios

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
}

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
}

How to display title for header text for header in first section iOS

Using method below of viewForHeaderInSection i try and display an image in table header section one , but i also want a heading text, using the method of heading title text i am able to display the heading for all sections but not section 1 , do i need to make some changes to heading view for section one to be able to display the heading , if i were to remove the viewForHeaderInSection then the title for first header section also shows up
let sectionTitle = ["Feedback", "Follow Us"]
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0 {
headerTop.headerLogo.image = UIImage(named: "foodpin-logo")
return headerTop.topView
}
return nil
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionTitle[section]
}
You need to create a text label in your headerTop view and remove this function override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int). So your code will be like this:
let sectionTitle = ["Feedback", "Follow Us"]
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
headerTop.headerLogo.image = (section == 0) ? UIImage(named: "foodpin-logo") : nil
headerTop.yourHeaderTextLabel.text = sectionTitle[section]
return headerTop.topView
}
It seems like you are looking for tableHeaderView! The issue is you are overriding the first section's header.
You may consider setting the tableView's tableHeaderView for the headerTop instead. So all sections will have their titles as you expected.
tableView. tableHeaderView = headerTop
Also, if you want yout table's header to be sticky, there are bunch of tutorials for that.

Swift UITableView (no sections) filter to table view with sections

I have a list of custom objects [Species] displayed in the table view, which is sorted alphabetically. Table has one section with no headers, it's one continuous list.
What I would like to achieve is, when a user selects the option to sort the data "by country", to do the following:
to sort array to find out how many sections I will need by - "Species.country"
to create sections with header titles of the country
to sort countries (Sections) alphabetically
reload table view to display sections
remove sections on the reversed action (sort entire list A-Z)
Is it possible to create dynamically sections when filtering/sorting? Can you please point me the right direction? Many thanks
A.
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 120
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.genusArr.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: CustomMenuCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomMenuCell
//genusArr is type of [Species]
let genus = self.genusArr[indexPath.row]
cell.populate(with: genus)
return cell
}
It's all in your data model. You always have sections with rows. But you only have one section. Modify your data model and your data source methods to always support multiple sections. Just sometimes this data model will only have one section.
Update your data model to be an array of dictionary where the dictionary contains a title and an array. The top level array is your sections (may just be one at times). The inner arrays (in each dictionary) are the rows of each section. Or define a struct with a title and array instead of using a dictionary.
With this in place, and your table view data source and delegate methods written to always work with multiple sections, your table will also work just fine when you happen to have just one section worth of data.
Now it's just a matter of populating your array of dictionary as needed depending on how you wish to organize the data for display.
To answer the question of section headers. You could try using:
tableView(_ tableView: UITableView, titleForHeaderInSection section: Int)
or
tableView(tableView: UITableView, viewForHeaderInSection section: Int)

Swift 4 UITableView Like Apple Music Search

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
}

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

Resources