Swift: Table view section titles - ios

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

Related

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 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
}

How do I use a line break in the footer title of a UITableView?

I provide a footer title for my table view like this:
override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
return "DEVELOPED BY COMPANY NAME\nIN COMPANY LOCATION"
}
When I run my code, only "DEVELOPED BY COMPANY NAME" shows up in the footer and the text beyond the line break is nowhere to be seen. How would I go about making sure that all of the text is seen?
If it is conform to your design you can change to a grouped table style in Interface Builder.
In storyboard - select your viewController - Select the table
Select the "Style" property in interface-builder
Select "Grouped"
Grouped Style provides per default a multiline Footer. Just override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String?
Thats it. No need for an extra view or providing the height.
The default footer view is a label with 1 line of text.
If you want a label with multiple lines you’ll have to create a view and return it in the function viewForFooterInSection.
You will also need to implement the function heightForFooterInSection to give the label the correct height. (Thanks rmaddy)

I want to add a sort and filter button on top of tableview in ios?

I want to implement Something like this two views for Sort and Refine on top of table view.
is there something called table header or something similar where i can add this .
this screenshot is from myntra app u can check that for reference.
Use a UIViewController with a container UIView on top and a UITableView in bottom
Something like in the screenshot
Grey part is the UITableViewController
Upper part is a UIStackView with 3 buttons in it.
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = UIView(frame: CGRect(0,0,view.frame.width,30))
return header
}
create a view with two buttons Sort and Filter and return in above function also dont forget to return height in below func
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
this will give you sticky header like Myntra app
OR return view in tableView.tableHeaderView = yourview but this will not stick and get scrolled along the scroll

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