I have successfully removed the colored separator at the bottom of my UITableView with:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0.1f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
return [UIView new];
//[[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
}
However when I deselect the last row, the footer gets drawn again.
How can I prevent the footer in my section 1 from ever being drawn please?
Related
Here is a basic tableview that I have set up with a tableviewcontroller. The reuse identifier just gets a light green prototype cell.
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 25)];
[self.tableView.tableFooterView setBackgroundColor:[UIColor colorWithRed:215/255 green:1 blue:1 alpha:1]];
}
- (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *removeFolderRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault
title:#"hi"
handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
return;
}];
return #[removeFolderRowAction];
}
- (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
return;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 4;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
return [self.tableView dequeueReusableCellWithIdentifier:#"cell"];
}
#end
This produces this flow:
where it starts with no bottom separator, then if you go to editing mode on the last cell there is still no bottom separator, but when done editing, a full width separator appears, which leaves if you go to editing mode again. If I were to have my tableFooterView be a view with frame CGRectZero, it would behave in the expected manner (there is a separator, same as all the other ones, that doesn't move when going to edit mode), however as soon as the tableFooterView has some height, it goes back to acting like the pictures above.
So i digged a bit and ios 11 fixed this issue for me. Seems to be a bug in previous versions.
For me the separator would show in the last rows that touch a header or a footer
I have a UITableView set to the UITableViewStyleGrouped style. Whenever a section has one or more items, it displays a one pixel separator beneath the section header. However, if the section has no items, there is no separator between sections.
How can I get the separator to appear in all sections, even those that have no cells?
Notice how section 1 and 2 have a separator between them and their first cell, but section 3 doesn't have a separator between it and section 4.
One solution is to add a footer to each empty section:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return [self tableView:tableView numberOfRowsInSection:section] == 0 ? 0.5 : 0.00000001;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
if ([self tableView:tableView numberOfRowsInSection:section] == 0) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.5)];
view.backgroundColor = tableView.separatorColor;
return view;
} else {
return nil;
}
}
Notes:
This uses the separatorColor to ensure it is consistent with the other lines in the table view.
Instead of returning 0 in heightForFooterInSection, it returns a very small number instead.
This produces correct results most of the time:
... but has several problems:
This exploits the footer, so if you need to display a footer, this solution probably won't work.
In edit mode, when you drag the last item out of a section, the border will disappear between that section and the one below it.
In edit mode, when you drag an item into an empty section, there will be a double border at the bottom of the section.
Example of problems 2 and 3:
In the above image, notice how there is no line between sections 2 and 3, since all the items were moved out of that section (problem 2).
Also notice how the last items in section 3 and 4 have double borders, since they were dragged into new sections (problem 3).
You can try customize your section header view. use this delegate funciton: - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
Use this two method to add separator between Section.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 44.0;
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
[headerView setBackgroundColor:[UIColor brownColor]];
UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 43, 320, 1)];
img.backgroundColor = [UIColor whiteColor];
[headerView addSubview:img];
return headerView;
}
This code creates a margin at the top and bottom of the UITableView. How can I modify it to do the same but between each individual UITableViewCell?
// set header height
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 10;
}
// set header colour
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] init];
headerView.backgroundColor = [UIColor lightGrayColor];
return headerView;
}
// set footer height
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 10;
}
// set footer colour
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] init];
headerView.backgroundColor = [UIColor lightGrayColor];
return headerView;
}
If successful, I'd also like to be able to add similar left and right margins if possible.
Use an extra Background UIView with top, left. bottom and right margin.[red background view in the picture]. Add all other subViews on this view.
I have a custom header bar in my UITableView that I want to overlap the cell below it. Does anyone know if this is possible? At the moment I've tried off setting the scroll up but doesn't seem to work does anyone have any ideas?
Here's what I have tried
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) return 200;
else return 100;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return #"";
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 20;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
headerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"hm_pullToRefreshBar.png"]];
return headerView;
}
EDIT: I've tried to make the header height 10 pixels shorter than the UIView that I've set for the header view, but that just cuts the graphic off instead of letting it overlap.
I have a grouped table view with a few sections. By default, there is gap between the sections. I've been looking around for a simple way to remove the gap completely without any luck. The best I could do through the Interface Builder is to set the "Section Height" values in the "Table View Size" property section, but these fields do not accept 0 as valid input. Can anyone tell me the best way to sort this out?
The above answer for me did not work, I had to actually create a UIView with a height of 1 and a background color of clear and then set it's y origin coordinate to -1.
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -1, tableView.frame.size.width, 1)];
[view setBackgroundColor:[UIColor clearColor]];
return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 1;
}
This gave me a Grouped tableView where the section cells would not scroll underneath the headerView and not have padding in-between the sections.
In your table view's delegate, you can implement tableView:heightForHeaderInSection: and tableView:heightForFooterInSection:.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 0.0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.0;
}