iOS - Custom section header bounds issue - ios

I'm trying to create my own section header for my grouped cells. When I create it, using the following code, it looks as if the bounds are off. More specifically the height of the CGRect
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *header = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 50)] autorelease];
[header setBackgroundColor:[UIColor clearColor]];
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 3, tableView.bounds.size.width, 18)] autorelease];
label.textColor = [UIColor redColor];
label.text = #"SOME TEXT";
label.backgroundColor = [UIColor clearColor];
[header addSubview:label];
return header;
}
Any idea why the height is off? Am I missing something?

Have you also implemented tableView:heightForHeaderInSection: as described in the docs? This should return 50 for your table.

Just curious as to why you aren't defining the TableView type (Grouped), setting the properties of the section headers in IB and then using:
- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

Related

Custom UITableView Section not increasing height

I'm trying to make a custom TableView Section. That is higher than the normal section header.
The problem is when i example increase the height from 18 to 30. Nothing happen it does not get any bigger. What am i doing wrong?
i have this code:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)];
/* Create custom view to display section header... */
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, tableView.frame.size.width, 18)];
[label setFont:[UIFont boldSystemFontOfSize:12]];
NSString *string =[[sectionsArray objectAtIndex:section] objectForKey:#"league"];
/* Section header is in 0th index... */
[label setText:string];
[view addSubview:label];
[view setBackgroundColor:[UIColor colorWithRed:166/255.0 green:177/255.0 blue:186/255.0 alpha:1.0]]; //your background color...
return view;
}
Implement this method :
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 18.0; // whatever height you want
}
If you are using iOS 7, you can use implemeent the following method also :
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection: (NSInteger)section NS_AVAILABLE_IOS(7_0);

uitableview header background not shown until scroll

I used the following code to set the header view of a uitableview section. The problem is, the background color (mainLightColor_2) doesn't show up until I scroll the tableview. How can I make the background color shown on load?
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 24)];
headerView.backgroundColor = [Utility mainLightColor_2]; //mainLightColor is a blue color
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 2, 200, 20)];
label.backgroundColor = [UIColor clearColor];
label.textColor = [Utility mainDarkColor];
label.text = #"Past Activities";
[headerView addSubview:label];
return headerView;
}
Before scroll:
After scroll:
You should also implement - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section method.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 24;
}
So I found a solution to this. However, I still don't understand why setting the background color of headerView doesn't work.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 24)];
UIView *headerViewBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 24)];
headerViewBackground.backgroundColor = [Utility mainLightColor_2];
[headerView addSubview:headerViewBackground];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 2, 200, 20)];
label.backgroundColor = [UIColor clearColor];
label.textColor = [Utility mainDarkColor];
label.text = #"Past Activities";
[headerView addSubview:label];
return headerView;
}

Custom table section footer view - label not visible

I created a new Single-view project (using storyboard) in Xcode with a single UITableViewController. Here is the setup code:
- (void)viewDidLoad {
[super viewDidLoad];
_footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
_footerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, 44, 44)];
l.text = #"Label Label Label Label Label Label Label Label Label";
l.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
l.backgroundColor = [UIColor clearColor];
[_footerView addSubview:l];
_footerView.backgroundColor = [UIColor lightGrayColor];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return _footerView.frame.size.height;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
return _footerView;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
return [tableView dequeueReusableCellWithIdentifier:#"Cell"];
}
I want the label in the custom table footer view to be drawn at x = 60, but when I run the project, at first the label is invisible (in portrait, screen attached). Then if I rotate once, it becomes visible and if I rotate back to portrait it's visible.
What am I missing?
You seem to be initialising your footer view with a width and height of 44px, yet adding the label outside of its bounds.
Try the following instead:
_footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.tableView.frame), 44)];
_footerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UILabel *l = [[UILabel alloc] initWithFrame:CGRectInset(_footerView.bounds, 60.0f, 0.0f)];
l.text = #"Label Label Label Label Label Label Label Label Label";
l.autoresizingMask = UIViewAutoresizingFlexibleWidth;
l.backgroundColor = [UIColor clearColor];
[_footerView addSubview:l];
_footerView.backgroundColor = [UIColor lightGrayColor];
As a further point, try not to use [UIColor clearColor] as a label background colour if you can help it - it significantly reduces scrolling performance. In this case you should use [UIColor lightGrayColor] so it matches its superview.

custom header overlaps cells in group

Im a bit stuck trying to create a custom ios table header. My headers are 30px tall and I am using this code to create them:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil) {
return nil;
}
// Create label with section title
UILabel *label = [[UILabel alloc] init] ;
label.frame = CGRectMake(0, 0, 140, 30);
label.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"header_gradient.png"]];
label.textColor = [UIColor whiteColor];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0.0, 1.0);
label.font = [UIFont boldSystemFontOfSize:12];
label.text = sectionTitle;
// Create header view and add label as a subview
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 140, 30)];
[view addSubview:label];
return view;
}
- (CGFloat)tableView:(UITableView *)tableViewheightForHeaderInSection:(NSInteger)section {
return 30;
}
It almost works, but my headers seem to colide with the cells/headers below them:
Can anyone point me in the right direction on cleaning up the headers here?
Thanks in advance
You're missing a [space]:
You have:
- (CGFloat)tableView:(UITableView *)tableViewheightForHeaderInSection:(NSInteger)section {
It should be:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

Custom table header section

I got an app with group style table.
I tried to customize table header section:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
UILabel *userName = [[UILabel alloc] initWithFrame:CGRectMake (0,0,200,30)];
if (section != 0)userName.text = #"lalala";
[userName setFont:[UIFont fontWithName:#"HelveticaNeue" size:20]];
[headerView addSubview:userName];
[headerView setBackgroundColor:[UIColor clearColor]];
[userName setBackgroundColor:[UIColor clearColor]];
return headerView;
}
But my headers close cells:
Why?
You also need to implement tableView:heightForHeaderInSection: to set the correct height for the section header. See the Apple docs
This is happening because the height of your header is set to 0 until you override the other delegate:
– tableView:heightForHeaderInSection:

Resources