Renaming header of section of uitableview - ios

How can i rename the title of section header on uitableviewcontroller outside of titleForHeaderInSection ?

So as per the question statement update title without reloading or delegates just using the label reference for updating the title for section.
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView() //set the frame accordingly
let label = UILabel() //set the frame accordingly
//make it global and use it reference for updating the title of the section
view.addSubview(label)
return view
}
I am not using Xcode so please validate just writing code roughly

// If only section 2 has a header
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return section == 2 ? updateHeader(section) : nil
}
func updateHeader(section: Int) -> CustomHeader {
let header = (tableView.footerViewForSection(section) ?? tableView.dequeueReusableHeaderFooterViewWithIdentifier("customHeader")) as! CustomHeader
// Set up your header
return header
}
// Change text in header
let header = updateHeader(2).customLabel.text = "Your custom text"
Just a small example. Be aware that when the tableview gets reloaded, it will change back to your default setup of your header. So be sure you handle that within your own code.

You can do it without using titleForHeaderInSection
Make dynamic array 1st object for header custom cell then objects for tableview cell... and so on
Draft custom cell for header
While screen appear use cellForRowAtIndexPath with condition as per header custom cell and objects for table view
At particular event change array value for header custom cell then use reloadRowsAtIndexPaths()
So it will reload only index given in reloadRowsAtIndexPaths(), no need for reloadData().

Related

Auto resize font in sectionHeader

I Want to show a name and some other short informations in the sectionHeader of my Tableview.
Some of the names are very large so they don't fit, is there a way to autoresize the fontsize in the sectionHeader like in a label with:
label.adjustsFontSizeToFitWidth = true;
The easiest way would be to create a UILabel in tableView(_:viewForHeaderInSection:)
Apple Docu Discussion:
The table view uses a fixed font style for section header titles. If
you want a different font style, return a custom view (for example, a
UILabel object) in the delegate method
tableView(_:viewForHeaderInSection:) instead.
Simply follow the code for custom header view -
Setup tableView
tableView.estimatedSectionHeaderHeight = 80 // You can change this value accordingly
tableView.sectionHeaderHeight = UITableViewAutomaticDimension
Implement your custom section header and return it in the delegate method
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
switch section {
case 0:
return sectionHeader
default:
fatalError("Unreachable code")
}
}
Finally, if the contents in the header section changes while the header is presented, after the change you will have to tell the tableView to redraw itself using
func refreshTableAfterCellExpansion() {
self.tableView.beginUpdates()
self.tableView.setNeedsLayout()
self.tableView.endUpdates()
}

How To Get Rid Of Extra Space Above The UITableViewStyleGrouped Section?

We're want to do two changes to our search results.
Change the Collection View Controller to a Table View Controller
Have only the second section header show and have it scroll away inline. In other words, don't be sticky and don't stay at the top.
In regards to changing the Collection View Controller to a Table View Controller. This involves changing the section headers from a UICollectionReusableView subclass to something else. Normally, the change would be to using a UITableViewHeaderFooterView.
In regards to making the second section header scroll on up inline and out of sight and thus not be sticky, there's an issue to my solution.
To make it non-sticky, I changed the tableview to be UITableViewStyleGrouped. This causes a look for the second section that is different than what we have now. We would like it back to how it looks now.
Here's what we have in the App Store now:
Here's what we have when trying the UITableViewStyleGrouped approach and other changes:
Notice the extra gray above the "Other cars..." section header? I figure I can probably solve the rounded corners and insets via a subclass of the UITableViewHeaderFooterView. However, it's not clear how to handle the extra space above the section.
Code:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.estimatedRowHeight = 800
self.tableView.rowHeight = UITableViewAutomaticDimension
let nib = UINib(nibName: "SearchResultsOtherCarsHeader", bundle: nil)
tableView.register(nib, forHeaderFooterViewReuseIdentifier: "SearchResultsOtherCarsHeader")
:
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section != 1 {
return 0
}
return 30
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section != 1 {
return nil
}
let searchResultsOtherCarsHeaderView = self.tableView.dequeueReusableHeaderFooterView(withIdentifier: "SearchResultsOtherCarsHeader") as! SearchResultsOtherCars
return searchResultsOtherCarsHeaderView
}
How to get rid of the extra gray space above the section header?
The solution:
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0.01
}
This solution was inspired by the comment made by #maddy and iPhone UITableView : How to remove the spacing between sections in group style table?

How to prevent tableview section head from sticking while scrolling?

I have a tableview with many sections. I am using an actual tableview cell which I dequeue to use as a custom section header. The issue I am having is when I scroll the section header "sticks" to the top of the table until the next section appears.
How can I prevent this from happening and actually have it scroll up like normal?
Here is my code
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 3 {
let cell = tableView.dequeueReusableCell(withIdentifier: "headerCell") as! HeaderTableViewCell
return cell
}
return UIView()
}
Do following steps.
From #IB
Select your tableView
Go to Attribute Inspector
Find Style Attribute drop down which is Plain by default
Change it to Grouped
Or if you are creating TableView Programmatically use
let tableView = UITableView(frame: YourFrame, style: .Grouped)
Change UITableViewStyle to UITableViewStyleGrouped.
Set UITableViewStyle to UITableViewStyleGrouped, the headers will scroll up with the cells.
and
func tableView(_ tableView: UITableView,
heightForFooterInSection section: Int) -> CGFloat
{
return 0.000001;
}
Step1: Go to Mainstoryboard
Step2: Click on Your Tableview
Step3: Go to Attribute Inspector
Step4: Find Style Attribute drop down which is Plain by default
Step5: Change it to Grouped
Some possible solutions :
If you have just one section then set the Tableview Header property to the UIView you want to set as the Section Header.
If you have multiple sections, change the row count for each section to (number of rows for that section + 1) and make the row at 0th index to be a header. Some hand coding will be required.
Change your tableview to grouped, will require UI redesign so that the design looks similar to a plain tableview cell.
Instead of UIHeaderViewCell, You should use a UITableViewCell. Make section header height as 0 and for every 0th item in that section , display a UITableViewCell with heading on it

Customizing Header and Footer of Table View with Swift

I need to make a custom header for my tableview. for this I created a class of UITableViewHeaderFooterView type, but I can not select the header of the tableview on the storyboard to set up the class. if it were a static table header appear, but it is not visible to dynamic tables. How can I make this setting?
Note: I use xcode 7.3.1
I'm trying to do something like this, but the storyboard:
https://github.com/jeantimex/ios-swift-collapsible-table-section
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterViewWithIdentifier("header") as! NovaListaTableViewHeader
header.titleLabel.text = sections[section].name
header.arrowLabel.text = ">"
header.setCollapsed(sections[section].collapsed)
header.section = section
header.delegate = self
return header
}
There are several ways to do that. Most of do that by programatically and some achieve it via storyboard and program.
so its totally depends on you how you want to achieve it.
I can share a easiest way with you to customise your header and footer section.
if you have good control on storyboard try to create one
UITableViewCell
Now decorate it as you want and in identifier Name it SectionHeader now use it as reuse cell
After that use this Delegate Method i am sharing an objective C delegate,
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
var headerView: SectionHeaderTableViewCell? = tableView.dequeueReusableCellWithIdentifier("SectionHeader")
if (headerView == nil) {
headerView = SectionHeaderTableViewCell(style:UITableViewCellStyle.Subtitle, reuseIdentifier:"SectionHeader")
}
headerView!.textLabel!.text = "Hello World"
return headerView;
}
now do same thing for footer.

How to make the first row of an UITableView a header of a section?

I have an UITableView with custom cells. I would like that the first row is always visible. As I have only once section, I thought of making a header but in this case I don't really know how to do it?
Is it possible to make a header from the first row with the same gesture recognizers, same dataSource behind the rows, briefly, have the header exactly like th row, just as if the row was pined to the top of the tableView?
You should use a header, or a separate view outside the table view. You can use the same gestures (generally, though not the delete) and data source.
If you want it all, you could use 2 table views- the first with one section and one row, the second with all the other data. It would be easiest if your data source was broken down in a similar way in the view controller.
In either case you can achieve what you want, but not by flicking a switch, you will need to add some logic and different views to make it happen.
If you want to make one static cell that is pinned to the top but in all other ways the same to the others, you could simply add one to your numberOfRowsInSection
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataSource.count + 1
}
Then when you display the cells, check for the row number and always set the first row to contain your static header content.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row == 0 {
// Create or set static cell content.
}
}
The other way is to create a custom section header and set it using viewForHeaderInSection
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0 {
var view = UIView()
view.backgroundColor = UIColor.blueColor()
return view
}
return nil
}

Resources