tableview size adjusting while collapsing using tableview - ios

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

Related

How to prevent tableview section head from sticking while scrolling?

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

How to make a sticky footer and header with a tableview?

I have a UITableView in the storyboard with also a header and a footer defined as can be seen in the picture.
However, the header disappears when I move the cell (it seems the header is the top cell). Same thing with the footer. It appears after I have browsed through all cells.
I tried to move the header and the footer outside of the Cell hierarchy. But this resulted in being not visible any more.
How can I make the header and footer sticky?
Don't confuse the section header and footer with the table header and footer.
The section header and footer, in a non-grouped table, are pinned to the top and bottom of the table while you scroll.
But the table header and footer, as you rightly say, are sort of like cells: they are before the first section and after the last section, and they scroll with the table.
If that's not what you want — that is, if there is something you want to show above the table and below the table, all the time — then you need to put them above and below the table view as completely separate views:
thing above
table
thing below
Of course, if you do that, you can't use a UITableViewController. You'll have to have a normal view controller, with the table view as an embedded view and the table view controller as a child view controller.
When using static UITableViews, you can't add custom views as header. You need to set the header and footer like this:
//Header
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
//return your custom view here
}
//Footer
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
//return your custom view here
}
You also have to specify the height of your header/footer:
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
//return the height
}
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
//return the height
}

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.

How to make the first row of an UITableView a header of a section?

I have an UITableView with custom cells. I would like that the first row is always visible. As I have only once section, I thought of making a header but in this case I don't really know how to do it?
Is it possible to make a header from the first row with the same gesture recognizers, same dataSource behind the rows, briefly, have the header exactly like th row, just as if the row was pined to the top of the tableView?
You should use a header, or a separate view outside the table view. You can use the same gestures (generally, though not the delete) and data source.
If you want it all, you could use 2 table views- the first with one section and one row, the second with all the other data. It would be easiest if your data source was broken down in a similar way in the view controller.
In either case you can achieve what you want, but not by flicking a switch, you will need to add some logic and different views to make it happen.
If you want to make one static cell that is pinned to the top but in all other ways the same to the others, you could simply add one to your numberOfRowsInSection
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataSource.count + 1
}
Then when you display the cells, check for the row number and always set the first row to contain your static header content.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row == 0 {
// Create or set static cell content.
}
}
The other way is to create a custom section header and set it using viewForHeaderInSection
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0 {
var view = UIView()
view.backgroundColor = UIColor.blueColor()
return view
}
return nil
}

Table view footer disappearing when removing cells

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]];

Resources