UITableView section header inset missing - ios

How do I fix the problem illustrated in the image?
The section header for the tableview is missing an inset.

You probably set the separator insets to 0, either in code or in the Interface Builder (can be found in the Attributes inspector:
This also causes the titles to have no inset. The default values are 15 for left and 0 for right.

[Could you post your code of UITableViewDelegate?
In UITableView, there is no API for you to set this insets in section header, so you could return a custom UIView in tableView:viewForHeaderInSection: then set the layout you want.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [UIView alloc] init];
UILabel *headerLabel = [UILabel alloc] init];
headerLabel.text = #"xxx";
[headerLabel sizeToFit];
headerLabel.frame = CGRectMake(20, 0, CGRectGetWidth(headerLabel.frame), CGRectGetHeight(headerLabel.frame));
[headerView addSubview:headerLabel];
headerView.frame = CGRectMake(0, 0, CGRectGetWidth(tableView.bounds), CGRectGetHeight(headerLabel.frame));
return headerView;
}

Related

How to show a footer view with custom text at the end of the UItableview

I have a UITableView to display search results. The user loads more results by scrolling to the end. When server has no more results, I want to show a text at the end of the tableview, that says "No more search results". I tried following code, but it is not working. Any suggestions on what is happening will be greatly appreciated.
UILabel *endLabel = [[UILabel alloc] init];
endLabel.text = #"No more results to display";
[endLabel sizeToFit];
self.tableView.tableFooterView = endLabel;
[self.tableView reloadData];
Following code may help you to solve out problem.
You can write this code to your viewDidLoad.
UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
[footer addSubview:yourlabel];
[footer bringSubviewToFront:yourlabel];
yourlabel.text = #"whatever you want to write";
yourlabel.frame = CGRectMake(self.view.frame.size.width/2 - yourlabel.frame.size.width/2 , 30, yourlabel.frame.size.width, yourlabel.frame.size.height);
tbl_view.tableFooterView = footer;
However you need to increase the size of footer view of table.
I. Create a separate view in the xib of your viewController. By separate view I mean do not drag the view in the viewController's view as a subview, instead just drag it into the whitespace in your Interface Builder.
II. Now design the view i.e put a label in it that says no more data.
III. Now create an outlet of that view in your viewController.
#property(nonatomic, strong) IBOutlet UIView * tableFooterView;
IV. When there is no more data just write
self.tableView.tableFooterView = self.tableFooterView;
V. Set the width & height of the footer in viewDidLoad
self.tableFooterView.frame = CGRectMake(0, 0, self.tableView.bounds.size.width, 40);
VI. And when you do have data and don't want to show the no more data thing then
self.tableView.tableFooterView = nil;
//custom header for footer
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UILabel *endLabel = [[UILabel alloc] init];
endLabel.text = #"No more results to display";
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, self.tblView.frame.size.width, 40)];
[footerView addSubview:endLabel];
return footerView;
}
//Method To Get Height For Header and Footer
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 40.0;// your view height
}

Making a TableViewCell Section Footer Transparent

I am trying to make a section footer of a UITableViewCell transparent and this is what I am doing right now:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIColor *background = [UIColor clearColor];
UIImageView *imageView = [[UIImageView alloc] initWithImage:(UIImage *)background];
imageView.frame = CGRectMake(10,10,1,30);
return imageView;
}
But I am getting a problem from casting background to a UIImage. Is this possible in this way, is there a better way to cast it or am I simply going about this wrong? I am basically using the footer as a way to create a clear spacer between cells. Need some guidance on this.
This code will add clear footer to your section, where you can set the desired height to whatever you need. Below it has been set to 30, to match your code in the question.
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 30)];
footerView.backgroundColor = [UIColor clearColor];
return footerView;
}
Instead of type casting the colour into the UIImage, use the backgroundColor property :
Try this :
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10,10,1,30)];
imageView.backgroundColor = [UIColor clearColor];

How to dynamically set background color for UITableView section headers?

I'm working on implementing UITableView section headers similar to the Photos app on iPhone. The top header view should have a grey background and the other header views should have a white background. Based on that I have a couple of questions I need help with:
How do I find out which section header is on top (e.g. adjacent to the nav bar)? Note, that the navigation bar is translucent (meaning it displays cells below itself), so I can't go by finding visible cells and then retrieving section based on that.
What should be the approach to change background color of the top section header to a different color and then back to the original color when it's no longer a top one?
Make the property NSUInteger sectionPath and initialize it with 0 in viewDidLoad.
Create a headerView and for specific section change backgroundColor of that view.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 30)];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, headerView.frame.size.width, headerView.frame.size.height)];
headerLabel.textAlignment = NSTextAlignmentCenter;
headerLabel.text = [titleArray objectAtIndex:section];
if(section == sectionPath) {
headerLabel.backgroundColor = [UIColor grayColor];
}
else {
headerLabel.backgroundColor = [UIColor whiteColor];
}
[headerView addSubview:headerLabel];
return headerView;
}
To dynamically find the top-most header in UITableView during scrolling
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
for (NSIndexPath* i in [yourTableViewName indexPathsForVisibleRows]) {
sectionPath = [i indexAtPosition:0];
}
}

How to remove extra whitespace above a UITableView?

I'm have been trying to get the header of the table scrolling along with the UITableView. It's finally working, however, now there's a whitespace showing up right under the map view (see screenshot). I had difficulty getting the map in the table header section to scroll with the table and also to get the UIButton to trigger within the table header with a user touch (the UIButton simply expands the map to full screen.).
I asked a question related to this yesterday regarding the scrolling and button touch. Those are working now - I'm sure the coding could be done better, but this is what I have at the moment and have to get this done asap. If I can resolve this whitespace issue I can refactor and optimize the code later.
thanks for any help with this.
notice the whitespace under the map and above the table view in the screenshot - I want to bring the table view so it's close to the map.
in my viewDidLoad: method
// mapview
self.topMapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 140)];
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.height,self.view.frame.size.height)];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.tableView];
then in the viewForHeaderInSection: method (It was suggested that this be done in the viewDidLoad: method and I tried that but it gave me more problems
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 140)];
UIButton *showMapButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
showMapButton.frame = CGRectMake(0.0, 0, 320.0, 140.0);
[showMapButton setTitle:#"show map" forState:UIControlStateNormal];
[showMapButton addTarget:self
action:#selector(mapDetailViewDisplay:)
forControlEvents:UIControlEventTouchDown];
[headerView addSubview:self.topMapView];
[headerView showMapButton];
self.tableView.tableHeaderView = headerView;
return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 140;
}
Just add this in you ViewDidLoad method
self.automaticallyAdjustsScrollViewInsets = NO;
[UITableView tableHeaderView] is something different than viewForHeaderInSection but you are setting the same object to both of them.
You should rather set self.tableView.tableHeaderView in viewDidLoad: or return a headerView through viewForHeaderInSection delegate method depending on what kind of behaviour you are trying to achieve.
try to move all code in - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section leaving only return headerView
in viewDidLoad
and change
UIView *headerView = [[UIView alloc] init];
instead of
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 140)];

Determine custom section header frame when tableView mode grouped

I provide a custom section header via the tableView's delegate method. It works fine in plane mode but I can't figure out the correct margin in grouped style.
Does anyone know how to make the section header aligned with the tableView cells?
You can create a custom label and adjust its frame accordingly.
For eg.
- (UIView *)tableView:(UITableView *)aTableView viewForHeaderInSection:(NSInteger)section
{
UIView *view=[[UIView alloc] initWithFrame:aTableView.tableHeaderView.frame];
UILabel *label=[[UILabel alloc] initWithFrame: CGRectMake(//set frame of the label as you want)];
[label setFont:[UIFont fontWithName: size:]];
[label setTextColor:[UIColor blackColor]];
[label setShadowOffset:CGSizeMake(0.0f, 1.0f)];
[label setShadowColor:[UIColor redColor];
label.backgroundColor=[UIColor clearColor];
if(section==0)
{
[label setText://set your section0 title];
}
else if(section ==1)
{
[label setText://set your section1 title];
}
[view addSubview:label];
[label release];
return [view autorelease];
}
Hope this helps :)
Not tested in every flavor but a good starting point.
In
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
pass the tableView.style parameter to your own method providing the header.
Create a view a frame like
CGRect(0,0,CGRectGetWidth(tableView.frame), CGRectGetHeight(tableView.frame)
but add a subview with a frame
CGRectInset(frame, 30,0)
if you have a grouped tableView style. Set a the autoresizingMask to flexible width and it works.
Slightly modified in a way that I had to give an additional parameter to the creation method for the SectionHeaderView as the margin is different for presentationStyle FullScreen and Formsheet.
CGFloat margin = 0;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
margin = style == UITableViewStyleGrouped ? modalStyle == UIModalPresentationFormSheet ? 28 : 38 : 0;
}
else {
margin = style == UITableViewStyleGrouped ? 5 : 0;
}
UIView *view = [[UIView alloc] initWithFrame:CGRectInset(frame, margin, 0)];
view.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self addSubview:view];

Resources