I have a custom tableView with the willDisplayHeaderView below.
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let tableViewHeaderFooterview = UITableViewHeaderFooterView()
tableViewHeaderFooterview.textLabel?.font = UIFont.hmFont(13)
tableViewHeaderFooterview.textLabel?.textColor = UIColor.hmDarkGrey()
}
I have another tableViewController that subclasses from the custom tableView with the titleForHeaderInSection below.
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section == 0 {
return "YOUR SUBSCRIPTION"
} else if section == 1 {
return "SIGN OUT"
}
return nil
}
My willDisplayHeaderView is called but I am not seeing the changes in the header view titles when I run my app on simulator. I have been searching for answers the past couple hours on here but so far no solution has worked. I thought at first I might be forgetting to set my delegate/datasource but that didn't work.
Let's take a look at your code
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let tableViewHeaderFooterview = UITableViewHeaderFooterView()
tableViewHeaderFooterview.textLabel?.font = UIFont.hmFont(13)
tableViewHeaderFooterview.textLabel?.textColor = UIColor.hmDarkGrey()
}
In the function declaration you've got a view called view that your function is being called with. In other words, iOS is looking up this function on your class and saying, "I'm about to show this view, just so you know." In the first line of your implementation though, you're creating a new view called tableViewHeaderFooterview and setting some properties on it. Those changes are not going to be reflected in the view that iOS is telling you about.
At this point you have a few choices: 1) muck around with view's subviews looking for labels (messy and bad), 2) try to cast it to UITableHeaderFooterView (if let header = view as? UITableViewHeaderFooterView { header.textLabel.font = whatever }), or 3) register your own UIView subclass and configure it how you want ahead of time:
class MyHeader: UITableViewHeaderFooterView {
...
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.registerClass(MyHeader.self, forHeaderFooterViewReuseIdentifier: "myHeader")
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "myHeader") as? MyHeader {
myHeader.label.text = "YOUR SUBSCRIPTION";
return myHeader
}
return nil
}
Related
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.
I need an advice with ViewContoller's scheme. I should create a view with Billing addresses. There can be no address at all, or some addresses. If there no one, there should be only a button "Add New". And if there are addresses, each should have buttons Edit, Remove, and "Add New" too.
I have data for this VC as JSON, parsed and saved to plist.
So what is logic to make this View looks different depends on 1) if there are addresses or not? and 2) if there 1, or 2, or maybe 20 billing addresses?
Thanks a lot!
I solved issues like this with UITableVIew and the UITableViewDataSource and UITableViewDelegate:
setup the table view for one section (adresses)
func numberOfSections(in tableView: UITableView) -> Int {return 1;}
return the address arrays length in the delegate method
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return adresses.count
}
set the view for footer if the arrays length is 0
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
if adresses.count == 0 {
let vw = YourViewClass()
//I use blockskit library here (vw.bk_) to recognize a tap, but you can add a button by yourself
vw.bk_(whenTapped: {
//Create and present your next viewcontroller to
})
return vw
}
return nil
}
set the footers height to 0 if there are addresses
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
if addresses.count > 0 {
return YOUR_DESIRED_FOOTER_HEIGHT_FOR_INPUT
}
return 0
}
create row for each address
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let address = addresses[indexPath.row]
let tableViewCell = UITableViewCell() //maybe you have to create your own if the layout does not fit
//set tableViewCell's title / description to show address values
return tableViewCell
}
In this case, the footer view (you can do the same in the header if you want) with the add button is shown when no addresses are available, and it is hidden, when addresses are available.
I am developing an application in Swift3 where I have to show and hide UITableView Header for different users. For displaying UITableView Header View, I have created a custom class CustomHeaderCell of UITableViewCell.
Here is my code:
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 235.0
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell") as! CustomHeaderCell
return headerCell
}
Now Can anyone please help me to hide this Header of my UITableView?
Note: I tried using this tableView.tableHeaderView?.isHidden = true, but not working. Should I need to do the validation in heightForHeaderInSection?
Reference Link to Add HeaderViewCell: http://www.accella.net/knowledgebase/custom-header-and-footer-views-for-uitableviews/
If you have a way to differentiate users then you can just change the header height like this
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if userA {
return 235.0
} else {
return 0
}
}
That should help in hiding the header
You are mixing tableHeaderView and section header views, which are differents:
tableHeaderView is a view showed as a header for the whole UITableView
section header views are reusable views used for displaying header above each section
In your case, you want to use section header views, so you should return empty ones for non concerned users (I assume here sectionNeedHeader will be replaced by your condition).
Also, you better use UITableViewHeaderFooterView instead of UITableViewCell. The behavior is globally the same but it's made for this usage:
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if sectionNeedHeader {
return 235.0
}
return 0.0
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if sectionNeedHeader {
let headerCell = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderView") as! CustomHeaderView
return headerView
}
return nil
}
I have a UITableviewController which I created programmatically in Swift4.
The marked rectangle in the picture is my footerview with a UITableView inside
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let tableFooterView = UITableView()
tableFooterView.rowHeight = 100
return tableFooterView
}
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 365
}
I want to load custom cells in the tableview cells in footerview. I tried using this following code
tableFooterView.dequeueReusableCell(withIdentifier: emptyCellIdentifier) as! EmptyTableViewCells
But I get error -
fatal error: unexpectedly found nil while unwrapping an Optional value
Please help
You have to register the cell for the footer tableView when you are creating in viewForFooterInSection method and you should distinguish both the tables via tag or something if you are making the delegate and datasource as self for the tableFooterView UITableView
tableFooterView.register(EmptyTableViewCells.self, forCellReuseIdentifier: emptyCellIdentifier)
So your method will be :
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
if tableView.tag == 1 {
tableFooterView = UITableView()
tableFooterView.register(EmptyTableViewCells.self, forCellReuseIdentifier: emptyCellIdentifier)
tableFooterView.rowHeight = 100
tableFooterView.tag = 2
tableFooterView.dataSource = self
tableFooterView.delegate = self
return tableFooterView
} else {
//footer view is nil for the table view which is in the main table footer
return nil
}
}
This will stop the crash on the tableFooterView.dequeueReusableCell line but you need to add the other views like buttons and labels on the tableFooter Empty Cell by code and not IBOutlet. It will again crash if you access any IBOutlet connected component.
Coding in Swift 3. Have a tableView with custom cells and header.
I have a tableView with custom cells and headers. The headers have two (2) labels in them and have dynamic cell heights since the labels may be long. My problem is the first time the tableView and sections are configured the label appears as it should, HOWEVER, after scrolling down and then back up the headers' layout somehow breaks.
As you can see below, after I scroll down then back up to the cells, the label is getting cutoff.
After printing out what methods are being called I found that the first time scrolling down the tableView the following two (2) override functions are called.
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
print("\(section) heightForHeaderInSection")
print("\(section) returning AUTO for header")
return UITableViewAutomaticDimension
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
print("\(section) viewForHeaderInSection")
let header = tableView.dequeueReusableCell(withIdentifier: "QuestionHeader") as! QuestionHeader
header.delegate = self
header.contentView.backgroundColor = UIColor.groupTableViewBackground
header.questionTextLabel.text = String(questionStringArray[section])
header.questionNumberLabel.text = (String(section + 1) + ")")
return header.contentView
}
But when i scroll back up ONLY the viewForHeader function is called and I think because the height is no longer being set to UITableViewAutomaticDimension the labels get cutoff?
Any ideas?
You should return header instead of header.contentView from tableView: viewForHeaderInSection: method:
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableCell(...
...
return header
}