Where do a UITableView's section footers get their width? - swift

I am having an issue where the section footers for my UITableView are not taking up the full width of the table view. I have a custom cell that uses the text label for a descriptor and the accessory view for a UISwitch. It seems like the footer is using the width of the content view of these cells (though I should note it's slightly off), which does not include the accessory view.
Here's an image of the issue.
Is there something that is known to cause this that I can fix without having to do some weird workaround? I only phrase it this way as I just want the default behavior.

I tried this in a sample table view and it worked for me very well. I will just post what I did and maybe you can check if you have done the same.
Firstly, I set the Table View Style to grouped in the Interface builder:
Next, code for the table view data source and delegate methods:
ViewController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Cell implementation here
return UITableViewCell()
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "DATES"
}
func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
return "If both a delivery date and notification date are chosen, the notification date must be after the delivery date."
}
}
Note
Use tableView(_:, titleForHeaderInSection:) and tableView(_:, titleForFooterInSection:) for simple text as header and footer
Use tableView(_:, viewForHeaderInSection:) and tableView(_:, viewForFooterInSection:) when you need a non-text header and footer or a complex header/footer.

Related

How can I fix the position of a searchbar in a tableViewController

I'm trying to fix the position of a search bar in a tableViewController, I know how to do it in the navigation bar, but I can't find any documentation on how to do it without a navigation bar.
1.Create cell xib and set the search bar in cell ( ex: your cell name is SearchBarCell )
2.Now set the SearchBarCell as "viewForHeaderInSection"
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCell(withIdentifier: "SearchBarCell") as! SearchBarCell
return cell
}
give section height
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}

Swift adding clickable tableview

When I create a tableview, I cannot click on any of the items I put on the tableview. I was wondering how can I create a tableview that has every item clickable and when the user clicks on an item( say a city name, for example) it redirects the user to a different viewcontroller. ( for example if there are 22 clickable items in the tableview, there will be a total of 22 new different viewcontrollers )
Thank you very much in advance!
There are three major functions that a UITableViewDataSource must contain for the table view to work properly with user interaction (Pressing each row, for example). These functions are:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
The function you are looking to use is the third one. It is called when a user selects a certain row by tapping it on screen. You can find out the row index with 'indexPath'.
If you would like to go to 22 different view controllers, you need to create a manual segue between each one and label them accordingly. Then, you would want to call each individual segue depending on which row was selected inside of that third function! You can call a segue with an identifier with the performSegue() function.
Note that the class that contains these functions must be of type UITableViewDataSource, and you should tell the table view that it is the data source in the ViewDidLoad() function like so:
tableView.dataSource = self
Here is what a simple code can look like:
import UIKit
class viewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
var identifiers = [String]()
override func viewDidLoad() {
// fill your identifiers here
tableView.delegate = self
tableView.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return 22
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "yourCellIdentifier") as! yourCell
// fill your cell's data in here
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
// here you can use someThing like an array of your segue identifiers
self.performSegue(withIdentifier: identifiers[indexPath.row], sender: self)
//Or you can just implement a switch with every case doing what you want that cell to do which i don't recommend if you have 22 rows
}
}

Tableview creates five rows on one row?

I have a tableView with a custom cell that I have told should give me 5 cells in return of this custom cell. The question now is that I am running the app and getting five rows on one row. I have changed from default size of cell to custom but that is nearly the only thing I have done. So now I wonder what can create this kind of problem?
The code of my tableView looks like this.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "theoryCell") as! theoryTVC
return cell
}
So the problem is you are using custom cell with custom height but you are not setting height for row in table view method.
Try this:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return 100.0 //Choose your custom row height
}

Swift: Add cells containing a custom label to an UITableView

How do I programmatically add cells to a UITableview and fill the cells with data from myArray[cellNumber].
The data in the array is of type String. The tableview is just an UITableView connected with an outlet.
All the examples I've found is either +30 lines or doesn't work...
I'm using swift 4 and UIKit.
In Xcode, use "File > New > File > Cocoa Touch Class".
Use UITableViewController as a base class
You will find a big template, just implement:
numberOfSections(in tableView: UITableView) -> Int, make it return 1. You just need one section for now.
tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int, make it return the size of your array
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell. Uncomment it, implement it.
NOTE: To implement tableView(_:cellForRowAt:), you must in your storyboard register a cell, and use its name in this function. Or programmatically register a cell using register(_:forCellReuseIdentifier:) .
Here is a more comprehensive guide iOS Getting Started Guide UITableView
Implementation example:
override func numberOfSections(in tableView: UITableView) -> Int {
return 1 // Only one section
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// "cell" is registered in the Storyboard
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
// The registered cell, has a view with tag 1 that is UILabel as an example
// IndexPath is a data structure that has "section" and "row"
// It located the cell in your tableview/collectionview
(cell.viewWithTag(1) as? UILabel)?.text = myArray[indexPath.row]
return cell
}
1.Your ViewController must conform to the UITableViewDelegate, UITableViewDataSource.
That means your class file would look like this
class MyCustomViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
2.You must assign the dataSource and delegate properties of your UITableView object to your viewController either from the Storyboard by dragging or in the code in viewDidLoad for example by typing:
myTableView.delegate = self
myTableView.dataSource = self
3.Your class must override the UITableView required delegates/datasource methods numberOfRowsInSection and cellForRowAt:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = myArray[indexPath.row]
return cell
}
Note that to use dequeReusableCell you must set a Reuse Identifier for the cell in the storyboard file.

UITableViewController delegate gives error in swift

I am getting the error below while making a simple code for UITableView in Swift Xcode 6.1
/Users/classic/Documents/CIPL/Demo Projects/DemoTableView/DemoTableView/ViewController.swift:11:1: Type 'ViewController' does not conform to protocol 'UITableViewDataSource'
/Users/classic/Documents/CIPL/Demo Projects/DemoTableView/UIKit.UITableViewDataSource:3:48: Protocol requires function 'tableView(_:cellForRowAtIndexPath:)' with type '(UITableView, cellForRowAtIndexPath: NSIndexPath) -> UITableViewCell'
Anyone knows why this issue is occurring ?
Did you declared the mandatory functions of UITableVIewDataSource protocol in your class ?
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
You have to implement all mandatory protocol method for tableview.
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayData.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Define your cell and return it
}