iOS UITableView Custom Footer with UIView - ios

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.

Related

Keyboard pushes FooterView up when its opening < TextView > Swift

I have a problem that I couldn't find any appropriate solution.
I have a tableView with custom cells which holds a textView inside.
also I have a blank grey footer for the section.
whenever I click on the textView and the keyboard pops up the footer in that section goes up and hides behind the textView field.
I don't know how to make that footer sticky so it won't move when the keyboard pops.
if anyone have a solution it will be great!
thank you!
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
if section == 0 {
let footerView = UIView()
footerView.backgroundColor = .red
return footerView
}else {
return UIView()
}
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 8
}
Set your tableView as grouped give height to the tableview footer and tableView Header in the tableview header,footer height functions.
If you are not using tableview header the set the height of the header to .leastNormalMagnitude .
This is the way to make the tableView Footer Static.

hiding tableView.tableFooterView causes my section headerView to disappear

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

I want to add a sort and filter button on top of tableview in ios?

I want to implement Something like this two views for Sort and Refine on top of table view.
is there something called table header or something similar where i can add this .
this screenshot is from myntra app u can check that for reference.
Use a UIViewController with a container UIView on top and a UITableView in bottom
Something like in the screenshot
Grey part is the UITableViewController
Upper part is a UIStackView with 3 buttons in it.
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = UIView(frame: CGRect(0,0,view.frame.width,30))
return header
}
create a view with two buttons Sort and Filter and return in above function also dont forget to return height in below func
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
this will give you sticky header like Myntra app
OR return view in tableView.tableHeaderView = yourview but this will not stick and get scrolled along the scroll

How to scroll up an entire table view in Swift?

I have a horizontal scrolling paged collectionview on top, and a tableview at the bottom, like this:
What I'm trying to achieve is that when the user scrolls up the table view, I want the whole page to scroll upwards. Currently, the tableview is confined to the bottom space when scrolling up. Lots of apps have this pattern, but the Fiverr app is an example, and here is a screenshot. The second picture is when I started to scroll up:
I am a complete beginner in Swift so I have no idea even what keywords to search for. What would be the easiest way to accomplish this?
I think you want to put all the views into a UIScrollView.
You can also put the UICollectionView into the table's header view. But if you have more components below the UITableView, I find it easier to just put the whole thing into a UIScrollView.
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
//build header view here
headerView.addSubview(collectionView)
return headerView
}
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
//Return the height of the headerview.
return height
}
Edited to answer comment :)

Trying to pin custom cell to bottom of UITableViewController to use as a footer

I'm using this code to display my custom footer:
override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCellWithIdentifier("footerCell") as! FooterTableViewCell
return cell
}
However, when I reach the bottom of the table the custom footer cell detaches and bounces from the bottom. How can I make the other cells bounce but the footer cell sticky? Is there an easy way to do this in interface builder?
Ideally I'd like to not have to embed or subview the tableviewcontroller. Thanks! CHM

Resources