Make UITableViewHeaderFooterView non sticky - ios

I am having a common footer for all cells and I am setting it with
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
I know that by default footers are sticky to bottom of UITableView, I dont want footers to stick to bottom of table, what is best way to achieve this?
One way I know is to add extra row and show footer there, But I am looking for cleaner approach than this.
Can anyone help me with this?

You can set UITableViewStyle as UITableViewStyleGrouped.
Use this initializer.
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style;
You can set this on storyboard.

You can create an extra uitableviewcell and append at the last of tableview. In the numberOfRowsInSection: method you can add one 1 like
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [objectArray count]+1 ;
}
in tableView:cellForRowAtIndexPath: method
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexpath.row == [objectArray count]) {
//create footer cell
}
else {
//show normal cell
}
}

If you happen to have static height for footer and not changing the contentInsets, you can try this (assuming footer height is 44):
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, -44, 0)

Related

Adding background for TableView sections iOS

I have a section with dynamic multiple rows and I need to separate all the section with the background as shown in the image below.
Specifically those border lines for all the section. Pease let me know how can I make it possible.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
{
return ProductArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSArray *arr = [ProductArray objectAtIndex:section];
return arr.count;
}
Please refer to this
.
I tried adding header and footer, But I am not understanding how to add that rectangle box image for the entire section.
Put View in your cell after make outlet of that view. And use to cellForRowAtIndexPath this method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"TableViewCell" forIndexPath:indexPath];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
cell.viewBackGround.backgroundColor = [UIColor redColor];
return cell;
}
For above requirement, I would like to suggest you the below approach.
create only one section with multiple rows.
Each row of UITableView will have UITableViewCell
Inside UITableViewCell create content view for which we are gonna add the border
Each row of UITableViewCell will have another UITableView(inside content view) whose methods are handled by UITableViewCell cells
Change UITableView (inside) height based on number of rows.
provide height of UITableViewCell based on the number of rows + orderID Header.

Spacing between cell of tableview?

I have to set spacing between each tableview cell say of 36 pts .I am using tableview rather than custom cell for creating the cell?
You can do this by having number of sections equal to number of items to display. Each section will contain only one row. You will then return transparent header for every section which will give illusion of cell spacing.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return <Your items count>;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return cellSpacingHeight;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView * headerView = [UIView new];
[headerView setBackgroundColor:[UIColor clearColor]];
return headerView;
}
A table view isn't like a collection view with a complex and capable layout specification, you can't specify arbitrary layouts and spacings.
So, use a collection view, or, if you want to continue with a table view, use a custom cell and add an empty section into each cell. Set the height of your cells to an appropriate value.

How to add Margins on UITableView to inset content

I have a table view and I want to include margins so that the table's content has some breathing room on the left and right, and also between cells.
I managed to do a table view like:
My Storyboard design is like:
What I did was I added a UITableView to the main view and gave a margin of 10. I added constraints as shown in figure. Changed the Seperator style to None.
Then I added two UITableViewCells.
For Holding the data with custom row height 70.0
Row with same background of parentView with custom row height of 10.0
And implemented the methods like below:
// Row count (Twice as needed, for showing the insect)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 15*2;
}
// Returns cell based on indexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
// Decides whether content or inset
if (indexPath.row%2)
{
cell = [tableView dequeueReusableCellWithIdentifier:#"ReuseInset"];
}
else
{
cell = [tableView dequeueReusableCellWithIdentifier:#"ReuseMe"];
cell.textLabel.text = #"MMP";
}
return cell;
}
// Returns custom row height based on indexpath
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{ if (indexPath.row%2)
{
return 10.0;
}
return 70.0;
}

Resizing a cell in a TableView

Im trying to make some of my cells bigger in height. i tried using
CGRect rect = cell.frame;
NSLog(#"before height: %f",rect.size.height);
rect.size.height +=20;
cell.frame = rect;
NSLog(#"AFTER height: %f",cell.frame.size.height);
in
cellForRowAtIndexPath
and
willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath
*)indexPath
The log shows that the values have changed but it doesnt show any change in the simulator.
Thanks for the help
use tableView:heightForRowAtIndexPath method.
Apple docs clearly explains what to do. UITableView class reference
Every tableView has a delegate property.Set it to your viewController and implement above method. Its signature is
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
So, based on indexPath, return whatever the height you desire.
If you want constant height for all rows, you can use rowHeight property of UITableView.
Use heightForRowAtIndexPath of UITableViewDelegate. Example:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return indexPath.row == _pages.count - 1 ? 408 : 450;
}
To make some cells bigger, you should implement the method tableView:heightForRowAtIndexPath:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (<SOMETHING>) {
return height1;
} else {
return height2;
}
}
tableView:cellForRowAtIndexPath: is for configuring the content of the cell that the tableView needs to display.
Note that tableView:heightForRowAtIndexPath: has performance implications if you have a really large table (1000+ entries), this method is called on every row when the table view displays.

Custom "Floating" UITableViewCells

I have been searching the web for an answer to this and I am sure it has an easy answer.
I am creating an UITableView in my app and I am wanting it to have "floating" table view cells and a menu at the top. Like this:
I am sure that these are custom UITableView Cells, but I am not sure how to create them like this and have them be dynamic in size based on the content and how to include a menu at the top that disappears/shows once the user scrolls down or up.
Any insight on this would be awesome!
This can be done fairly easily with a subclassed UITableViewCell in a grouped table view. The image below shows one I quickly made by dragging in various UI elements, and creating a custom class, which has nothing but IBOutlets in the .h file.
The label with the gibberish in it is tied to the gray view below and to the top of the cell, with no specific height set, so when the cell grows, it will grow. Here is the code I used to populate the table:
- (void)viewDidLoad {
[super viewDidLoad];
self.theData = #[#"One",#"Two",#"Three",#"Four",#"Five",#"Six",#"Seven",#"Eight",#"Nine"];
[self.tableView reloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.theData.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *s = #"asdfhfl fl flfh sflhsalfjh fajlhf lf asldf fh asljfafh sjlfh ajf fljf fasjlfhjfhjfhjsf hsjfhsjfhajsfh the end";
CGSize size = [s sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(281, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
return size.height + 130;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
RDCell *cell = [tableView dequeueReusableCellWithIdentifier:#"RDCell" forIndexPath:indexPath];
cell.lbl1.text = self.theData[indexPath.section];
cell.lbl2.text = #"asdfhfl fl flfh sflhsalfjh fajlhf lf asldf fh asljfafh sjlfh ajf fljf fasjlfhjfhjfhjsf hsjfhsjfhajsfhajlfjafh";
return cell;
}
Notice that I set the number of sections to the count of the array, so you get separate sections of 1 row each. The code in the heightForRowAtIndexPath is typical of the way you would calculate the cell height (except that you would normally use the index path and get a different string for each cell).
I think thats what you are looking for..
IBScrollViewFloatingHeader

Resources