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
}
Related
Is there are reason why there are height differences between what is shown and what has been set as a height? also there is a space between header and prototype cell which I cannot get rid of.
extension FirstTabThirdView: UITableViewDataSource , UITableViewDelegate {
func tableView (_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = headerView
return view
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellOne", for: indexPath)
cell.textLabel?.text = "hello"
return cell
}
}
I'm trying to make a custom xib cell resize based on really how much information the user enters.
I have tried setting the constraints of the xib cell, like this Constraints
I only have top and bottom constraints on the Label and the Date.
In the viewDidLoad() of the viewcontroller containing the cell and tableview i have
self.tableView.rowHeight = UITableView.automaticDimension
and I also have set
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 250
}
GIF of Resize Not Working
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
}
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.
I'm trying to set a transparent background to a TableViewHeader but without success.
First of all, would like to know if it's possible?
This is what I've done
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
header.contentView.backgroundColor = UIColor.clearColor()
}
My final objective is to add under my uitableview an UIView that'll host a GMSMapView and have a transparent HeaderView with a size of 100px.
Any help?
Thanks
You need to do following steps
1) in storyboard Add headerfooterview to tableview
2)In view did load write below code
tableView.registerClass(HeaderView.self, forHeaderFooterViewReuseIdentifier: "HeaderView")
headerView = tableView.dequeueReusableHeaderFooterViewWithIdentifier("headerView") as? HeaderView
3) add tableview delegate methods
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 110;
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
headerView.backgroundColor = UIColor.clearColor()
return headerView;
}
Hope this will help you
Steps to follow:
1) Drag and drop a UIView from object library to the top of the tableview to make a tableview header.
2)make a IBOutlet of the view in the Tableview controller.
3)lastly call this UITableview delegate Method
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
yourHeaderView.backgroundColor = UIColor.clearColor()
return yourHeaderView
}
// this is called when you need to make a transparent footer view
override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
footerView.backgroundColor = UIColor.clearColor()
return footerView
}
//for transparent your table view section header:
//In Swift 5
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
view.tintColor = UIColor.clear
}