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;
}
Related
I want to display footer text only at last section of the TableView.In TableView I have a lot of TableView section it come from API.But I need to display footer message only at bottom of the tableView section how to do it?
(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 540, 10)];
footer.backgroundColor = [UIColor clearColor];
UILabel *lbl = [[UILabel alloc]initWithFrame:footer.frame];
lbl.backgroundColor = [UIColor clearColor];
lbl.text = #"Your Text";
lbl.textAlignment = NSTextAlignmentCenter;
[footer addSubview:lbl];
return footer;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 10.0;
}
Currently Im used this code.This shows in all section bottom.I need to display only at bottom of the last tableview section.
try this
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 540, 55)];
if (section == array.count -1){
footer.backgroundColor=[UIColor clearColor];
lbl = [[UILabel alloc]initWithFrame:CGRectMake(0,0,540,40)];
lbl.backgroundColor = [UIColor clearColor];
lbl.text = #" Please add your message";
lbl.textAlignment = NSTextAlignmentCenter;
[lbl setNumberOfLines:10];//set line if you need
[lbl setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:14.0]];//font size and style
[lbl setTextColor:[UIColor blackColor]];
[footer addSubview:lbl];
self.jobsTableView.tableFooterView=footer;
}
return footer;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
if (section == [tableView numberOfSections] - 1) {
return 60.0;
} else {
return 0.0;
}
}
use
if (section == yourArray.count -1)
{
UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 540, 10)];
footer.backgroundColor = [UIColor clearColor];
UILabel *lbl = [[UILabel alloc]initWithFrame:footer.frame];
lbl.backgroundColor = [UIColor clearColor];
lbl.text = #"Your Text";
lbl.textAlignment = NSTextAlignmentCenter;
[footer addSubview:lbl];
}
return footer;
Try this,
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if (section == [tableView numberOfSections] - 1) {
return 10.0;
} else {
return 0.0;
}
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
if (section == [tableView numberOfSections] - 1) {
UIView *footer = ..
....
return footer;
} else {
return nil;
}
}
or you can set view as footer to tableview
UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 540, 10)];
....
[self.tableView setTableFooterView:footer];
Please try to use like this if you wanna footerview of tableview.
UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 540, 10)];
footer.backgroundColor = [UIColor clearColor];
UILabel *lbl = [[UILabel alloc]initWithFrame:footer.frame];
lbl.backgroundColor = [UIColor clearColor];
lbl.text = #"Your Text";
lbl.textAlignment = NSTextAlignmentCenter;
[footer addSubview:lbl];
footer.backgroundColor = [UIColor blackColor];
self.tableView.tableFooterView = footer;
In the screenshot below, the blue colored line is the header and footer of the sections of my tableView (In the tableView, I am treating rows as sections).
However, i want the blue line to be just below the row of the tableView (of same width as the row). Any idea how to do it??
-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 333, 1)] ;
headerView.backgroundColor = [UIColor blueColor];
return headerView;
}
-(UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section
{
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 333, 1)] ;
footerView.backgroundColor = [UIColor blueColor];
return footerView;
}
You can add UIView with Blue color above the UIView for which set the background color to Clear Color
-(UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section
{
UIView *dummyfooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 2)] ;
dummyfooterView.backgroundColor = [UIColor clearColor];
// Widht should be less than dummyfooterView
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, 320-20, 2)] ;
footerView.backgroundColor = [UIColor blueColor];
[dummyfooterView addSubview:footerView];
return dummyfooterView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 2;
}
I am not sure though, hope this may help you! The output will be like below screen. I have used Grouped tableview style here.
I think the best way is to add 1px view at bottom of cell at row at indexpath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MessageBoxCell";
LGMessageBoxCell *cell = (LGMessageBoxCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, cell.fram.size.height - 1, cell.fram.size.width, 1)] ;
headerView.backgroundColor = [UIColor blueColor];
cell.backgroundView = headerView;
return cell;
}
Set height for header and fooder
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 58;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 58;
}
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.
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 {
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