Table view footer disappearing when removing cells - ios

I have a table view with a few sections and in these sections I have a footer views. The thing is when I remove cells from one of the sections for some reason the footer in that section disappears. It returns as soon as I scroll the table view.
I have tried implementing tableView.beginUpdates() and tableView.endUpdates(). This brings the footer view back with an animation from the top of the screen. I have then tried turning animations off with UIView.setAnimationsEnabled(false) to prevent this but the problem stil persists. I simply want the footer view to move up with the rest of the sections and header views without disappearing.

Try setting footer by code:
override func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String? {
switch (section) {
case 4: return "Footer" }
default: ""
}
return title
}

Try This
Objective - C:
[tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectNull]];

Related

Expandable view within tableview

I currently have a table view controller which consist of not only the cells but also a UIView. Now, within that UIView there's a label which might have more than 1 line of text with a See More button. When I pressed that button, the button itself will disappear and the text.numberOfLines is set to 0 so the View should expand to show all text. Which doesn't seems to work in my case, The button does disappear though and the text just continues to the edge of the screen, truncated instead of extending down.
But when this whole UIView is outside the table view controller the functions above work just as expected, but not after I've moved them to within the table View right above the prototype cells. Any Ideas?
When you are setting up the tableviewcell, you have to specify a fixed height for the row with the delegate function
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) {
if expandedArray[indexPath.row] {
return 60
} else {
return UITableViewAutomaticDimension
}
}
Then keep an array of bools, to specify if the cell is expanded or not. Primarily set the bools to false indicating "not expanded".
Then when the use presses the "see more" button, make the bool in the array with the right index true, indicating that the cell is expanded. and call the below code updating the cell.
tableView.beginUpdates()
tableView.reloadRows(at: [IndexPath(row: index, section: 0)], with: .automatic)
tableView.endUpdates()
That should do the trick.
P.S: Don't forget to properly add constraints inside tableview cell.
Use UITableViewAutomaticDimension and reload your cell on click of see more button.

tableview size adjusting while collapsing using tableview

I am using collapsing header using tableview. When I collapse using tableview, tableview content moved to top. I want tableview content fixed to the screen even after collapsing. I mean after collapsing there is an empty space in view since tableview moved to top.Any Ideas?
You can use Table Header View with number of row just like this
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isCollepse
{
return noOfRow
}
return 0
}
and on click of header change isCollepse and reload Section of table view

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

Hide unhide Static Table View Cells with Swift

I am trying to make a '''Static Table''' with some cells that are expandable and collapsing . I override the ''': WithAnimation:)''' which should provide me some control on the animation transition. Unfortunately, the cells returned are blank, I don't mean empty, but the cells have disappeared and their location is left blanc in the table. When I try using the '''reloadSections( )''' rather, the whole section is also returned blank. After some recherche , I start to suspect that '''reloadRowAtIndexPath(​: WithAnimation:)''' and '''reloadSections( )''' works with table associated with a data source.
Am I right? If yes, how can I control animation of the cell I want to hide/unhide so that the transition is smooth?
Thanks (edited)
To get a smooth animation, simply reload the tableView with
tableView.beginUpdates()
tableView.endUpdates()
this will call the heightForRowAtIndexPath automatically and render a smooth transition.
To expand on irkinosor's answer. In this example I have 1 section with 3 static rows. When tableView is initially loaded, my UISwitch is off and I only want the first row (row 0) be visible (hide rows 1,2). When the UISwitch is on, I want to show (rows 1,2). Also, I want smooth animation having the rows accordion to hide or show.
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row > 0 && toggleSwitch.isOn == false {
return 0.0 // collapsed
}
// expanded with row height of parent
return super.tableView(tableView, heightForRowAt: indexPath)
}
In my switch action I initiate the tableView refresh
#IBAction func reminderSwitch(_ sender: UISwitch) {
// to initiate smooth animation
tableView.beginUpdates()
tableView.endUpdates()
}
You can obviously add more logic to the heightForRowAt function if you have multiple sections and more interesting row requirements.

How do i keep the header cell moving with the tableview cells in Swift 2.0

I am trying to create a tableview which starts the header in the middle of the tableview and then can be scrolled up with the added tableView Cells to the top then stops, and then the tableview cells can just scroll "under" it. I got the header to be in the middle of the tableview, but the tableView Cells just scroll on top of it, and the header doesnt really scroll to the top. it just stay where it is. I want to be able to let the header move as you scroll the list, and then stops when it reaches the top of the list, and then its just the tableView Cells that are moving afterwards.
I am using the latest version of XCode
GO TO STORYBOARD > SELECT VIEW > SELECT TABLEVIEW > PROPERTY INSPECTOR > CHNAGE STYLE TO GROUPPED from PLAIN
Don't forget to handle footerview. In Groupped mode a footer will appear and arrange background or other properties. They will change.
see Image :
You can do it using sections. If i understood your question correctly, what you want to do is, you have to display header in the middle of screen while having some cells above the header and some cells below the header, and when you scroll the tableView up your header should be scrolled to top and when it reaches to the top you want your header to keep at the top while cells should be scrolled underneath the header.
So to do this return two section from the data source
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
return 2
}
And in the data source return conditional cells
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
If(section == 1) {
return <Return number of cell you want to display above header>
} else {
return <Return number of cell you want to display below header>
}
}
Then override the delegate
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
if(section == 0) {
return nil //As you do not have to display header in 0th section, return nil
} else {
return <Return your header view>
//You can design your header view inside tableView as you design cell normally, set identifier to it and dequeue cell/headerView as:
//However this may result in unexpected behavior since this is not how the tableview expects you to use the cells.
let reuseIdentifier : String!
reuseIdentifier = String(format: "HeaderCell")
let headerView = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: NSIndexPath(forRow: 0, inSection: 0))
return headerView
}
}
Set conditional height as
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
{
if(section == 0) {
return 0
} else {
return <Return your header view height>
}
}
What we are doing here is, we are displaying header in second section which would have some cell, so you will see some cells in the tableView without header and below this cells you will see headerView with some cells and when you scroll your tableView up headerView will scroll with the cell till it reaches at the top.
Hope this will help you.

Resources