I have a tableViewController with one section. I have added a UIView directly to the tableView in storyboard and set outlets to my tableViewController for that view (headerView) and it's contents which are a segmented control and a searchBar.
In the tableView delegate methods I have the following for my header:
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return headerView
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 88
}
Everything is working fine except that there is no separator for the very last row of my tableView. This is expected and the workaround for me has always been the following in viewDidLoad:
self.tableView.tableFooterView = UIView(frame: CGRect.zero)
self.tableView.tableFooterView?.isHidden = true
For some reason, these two lines of code cause my headerView to completely disappear from the table.
I could create a tableViewCell and use that as my tableView header but there is a lot of delegation required for the searchBar and segmented control and that leads to other problems. Mainly that the searchBar wants to resignFirstResponder every time I reload my tableView. Again there are workarounds for that but it all starts to get a bit messy.
Just wondering why my headerView is disappearing and what I could do about it.
Remove that header view from tableview and add it on UIViewController i.e directly drag them adjacent to first responder and exit. Then using IBOutlet you can use it as Section view. Here it is being treated as footer view as well.
It is because you assign the new UIView object to table header view
If you want to toggle table header then you have to follow below step
1) keep the reference of you view which you want to set in table header
UIView footerView = your storyboard referance
2) when you want to hide then just assign nil to table footer
self.tableView.tableFooterView = nil
3) Now when you want to show then again assign your refence view
self.tableView.tableFooterView = footerView
Related
I have a form inside a tableview controller. The last 3 parts of the form are address textfield, map and a save button. When the user begins typing on the address field a uitableview will slide up covering the map to display different results. When I set the child tableview mapTableview's delegate and datasource to self this is when the problem would start, the screen just displays a white background. I tried different solutions but they don't work. Also tried this one but the data source must be coming from the tableview controller itself. Dynamic Tableview inside a Static tableview Cell
When I create an array of strings in the class Datasource and put the following codes below in my tableview controller, the strings get displayed in the mapTableView.
var dataSource = DataSource()
mapTableView.delegate = dataSource
maptTableView.delegate = dataSource
But since the data source must be coming from tableview controller, I tried to put the code below, and many other things as suggested in other posts, in my table view controller but the screen won't display anything, just all white. And I get these errors:
UITableView was told to layout its visible cells and other contents without being in the view hierarchy (the table view or one of its superviews has not been added to a window)
Detected a case where constraints ambiguously suggest a height of zero for a table view cell's content view. We're considering the collapse unintentional and using standard height instead
extension EditProfileTableViewController {
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return pois.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellResult", for: indexPath) as! MapTableViewCell
let poi = pois[indexPath.row]
cell.textLabel?.text = poi.title
cell.detailTextLabel?.text = poi.subtitle
cell.detailTextLabel?.numberOfLines = 0
return cell
}
}
and in my viewDidLoad
tableView.estimatedRowHeight = 60.0;
tableView.rowHeight = UITableView.automaticDimension;
How to correctly put a tableview inside a static tableview cell? Please help
You should create another table view controller for the inner table view. Let's call it MapTableViewController.
Add Container View into the static cell in storyboard.
Add Table View Controller into that container view, set its class to MapTableViewController.
Put all datasource/delegate methods for mapTableView from the EditProfileTableViewController into MapTableViewController. After that, EditProfileTableViewController should have no UITableViewDataSource and UITableViewDelegate protocol methods at all.
I want to be able to have all of my separators in a table view to be inset by a certain amount. This table view needs a custom header for each section. See the following image for the desired insets:
My storyboard is set up so that I have a UIViewController which contains a container UIView, which in turn has an embed segue for a UITableView. My table view specifies a prototype cell, and I have set in the storyboard a Grouped style, Default separator, with Automatic inset.
On my Table View Cell in the storyboard, I have a Custom separator inset of 26 on the left.
In my table view controller, I have implemented the following two methods:
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "ItemHeader") as! TableSectionHeaderViewCell
header.title.text = "My Custom Date Header"
return header
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 60.0
}
In a separate file I have subclassed UITableViewHeaderFooterView to just set a font, text color, and some constraints.
Now if I set a custom separator inset on the storyboard at the table view level with 26 inset from the left, either From Cell Edges or From Automatic Insets, nothing changes with the separators that appear below each of my headers. Similarly, if I set these values programmatically in the viewDidLoad method of my table view controller, like so:
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableView.automaticDimension
tableView.register(TableSectionHeaderViewCell.self, forHeaderFooterViewReuseIdentifier: "ItemHeader")
tableView.separatorInset = UIEdgeInsets(top: 0, left: 26, bottom: 0, right: 0)
tableView.separatorStyle = .singleLine
tableView.separatorInsetReference = .fromCellEdges
}
Nothing changes with the separator insets, even using the .fromAutomaticInsets option for the separator inset reference. Here is what I am actually seeing:
It seems that the first and last cells in a section are actually not respecting the separator insets. I thought originally that this was an issue related to having a header for each grouped section, but the separator on the final cell with no header below it seems to indicate otherwise. How can I make it so that these separators all are inset by 26pts? Thank you for any insight you can provide.
Check this thread then, I think it will help:
Grouped UITableview remove outer separator line
I can see that they removed the default separator (Using Clear Color) and put a custom view for the separator themselves.
The other option I can suggest is don’t use a group style Table view, use the plain one and handle it yourself.
I have tableView with one section and footer set up in storyboard. I'm trying to change the text in my table's footerView in viewDidLoad() with following code
tableView.footerView(forSection: 0)?.textLabel?.text = "Testing text"
but tableView.footerView(forSection: 0) == nil
Then on user's tap on a button I call function that changes footerView's text using the same code and it works perfectly.
func changeFooterText() {
tableView.footerView(forSection: 0)?.textLabel?.text = "Testing text after tap"
}
Why the footerView doesn't exist in viewDidLoad and how can I change it's text in viewDidLoad? Or should I change it in an other place? When the screen is loaded I need to have the text in footer that depends on other data on the screen.
Looking forward to read your kind advices.
You can use table footer view methods:
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView?
"viewDidLoad" is too early in the view life cycle. The UITableView has not built it's components yet.
You might be able to get away with moving the code to "viewDidAppear", however it would be better to build out your footers and headers the same way you build out other cells. The dequeueReusableHeaderFooterViewWithIdentifier delegate method is just for this.
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
I want to make a custom footer for my UITableView.
For that, I will need the position of the last cell in the UITableView and set my UIView under the TableView.
But I don't know how to get the position of the last cell.
Is it even possible to make this kind of custom Tableview-footer?
You can add a footer directly in the storyboard or xib--just drag your view so that it's inside your tableView, but after all of the tableView's cells.
You don't need to make custom footer or to know the position of your last cell. It is already available on UITableViewDelegate.
You need to make a custom UIView then on the callback of func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? return the UIView.
Example:
func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let view = UIView.init(frame: CGRectMake(0, 0, tableView.frame.size.width, tableView.frame.size.height))
view.backgroundColor = UIColor.greenColor()
return view
}
The above code will give you a green UIView for the UITableView. Just make sure you do not set the Footer height to 0 on Storyboard or on code.