Setting custom header view in UITableView - ios

I am trying to add a custom view to the header of each of my UITableView's sections. I am using this delegate method to return the desired view. It is working partly as it causes the cell sections to be spread out as if there was a header there, however neither the text or UIButton actually appear. I know this method is being called as I places an NSLog in the method to see if it was. Am I making some kind of silly mistake, or is this not the right way of doing this?
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView* customView = [[UIView alloc]initWithFrame:CGRectMake(0.0, 0.0, tableView.bounds.size.width, 44.0)];
customView.backgroundColor = [UIColor clearColor];
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 44.0)];
headerLabel.textColor = [UIColor darkGrayColor];
headerLabel.font = [UIFont boldSystemFontOfSize:16];
UIButton *headerButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// add button to right corner of section
return customView;
switch (section) {
case 0:
headerLabel.text = #"Team Name";
break;
case 1:
headerLabel.text = #"Captain";
break;
case 2:
headerLabel.text = #"Wicket Keeper";
break;
case 3:
headerLabel.text = #"Batting Order";
headerButton.center = CGPointMake( 160.0, 22.0);
headerButton.backgroundColor = [UIColor blueColor];
headerButton.tag = section;
[headerButton addTarget:self action:#selector(enableCellReordering:) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:headerButton];
break;
default:
break;
}
[customView addSubview:headerLabel];
return customView;
}

Found it, you return customView; before the switch statement.

You return your customview twice , one before switch statement one after switch statement twice just need to remove return customView; before switch (section)

-(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 =[list objectAtIndex:section];
/* 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;
}

What about the custom header origin.x and origin.y ?
when I set them non-zero, but it's still next to the ledt edge.

Related

UITableViewCell scroll over custom UITableView header title

I have a simple UITableViewController which contains about 40 UITableViewCells and the UITableViewController is embedded in a UITabBarController. The UITableViewController is created in Storyboard.
I am overriding the custom UITableViewHeader Text so that I can have a few guides there for users. I am noticing some really weird behaviour with this as represented by the following images:
As can be seen, the UITableView scrolls, but the header text remains there and it looks terrible.
Here is how I'm setting up the custom header:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// Custom label for the section header titles
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(25, 0, tableView.frame.size.width, 110)];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.numberOfLines = 0;
[label setFont:[UIFont systemFontOfSize:17.0]];
if (section == 0)
{
label.text = #" Add to this by marking a leaflet or video as a favourite.\n\nYou can do this by swiping left on any leaflet or video cell.";
}
return label;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
CGFloat headerHeight = 0;
headerHeight = 110;
return headerHeight;
}
I have worked with different sizes, but no matter what I do, it still behaves in the same way.
If anyone has any thoughts or guidance on this, that would really be appreciated. I just want the header text to also scroll up with the table view and to therefore be hidden from view when scrolling.
You can add your header as the tableHeaderView like this:
- (void)viewDidLoad {
[super viewDidLoad];
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(25, 0, self.tableView.frame.size.width, 110)];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor blackColor];
label.numberOfLines = 0;
[label setFont:[UIFont systemFontOfSize:17.0]];
label.text = #" Add to this by marking a leaflet or video as a favourite.\n\nYou can do this by swiping left on any leaflet or video cell.";
self.tableView.tableHeaderView = label;
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
// Custom label for the section header titles
UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 110)];
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(25, 0, tableView.frame.size.width, 110)];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.numberOfLines = 0;
[label setFont:[UIFont systemFontOfSize:17.0]];
if (section == 0) {
label.text = #" Add to this by marking a leaflet or video as a favourite.\n\nYou can do this by swiping left on any leaflet or video cell.";
}
[view addSubView:label];
[view setBackgroundColor:[UIColor blueColor]]; // the color for background
return view;}

UiTableViewHeader - Dynamic button event handler

I need to add a uiButton to a static uitableview section header - I've made an attempt with the following -
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
// you can get the title you statically defined in the storyboard
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
CGRect frame = tableView.bounds;
// create and return a custom view
#define LABEL_PADDING 10.0f
HeaderLabelStyling *customLabel = [[HeaderLabelStyling alloc] initWithFrame:CGRectInset(frame, LABEL_PADDING, 0)] ;
UIButton *addButton = [[UIButton alloc] initWithFrame:CGRectMake(frame.size.width-60, 10, 50, 30)];
addButton.backgroundColor = [UIColor whiteColor];
addButton.titleLabel.textColor = [UIColor colorWithRed:240/255.0f green:118/255.0f blue:34/255.0f alpha:1.0f];
addButton.titleLabel.tintColor = [UIColor blackColor];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 30)];
title.text = #"iawn";
customLabel.text = sectionTitle;
customLabel.backgroundColor= [UIColor colorWithRed:143.0f/255.0f green:137.0f/255.0f blue:135.0f/255.0f alpha:1.0f];
customLabel.textColor = [UIColor colorWithRed:255/255.0f green:255/255.0f blue:255/255.0f alpha:1.0f];
[customLabel addSubview:addButton];
[addButton addSubview:title];
[addButton addTarget:self action:#selector(receiverButtonClicked:) forControlEvents:UIControlEventTouchDown];
return customLabel;
}
-(void)receiverButtonClicked:(id)sender{
NSLog(#"button clicked");
}
the above adds a button - but doesn't react to the click event - can anyone suggest how I can get this to work?
UILabel does not handles touches by default.
Add following line of code:
customLabel.userInteractionsEnabled = YES;
In order to display button only for third section you should add following condition:
if(section == 2){...}
So your -tableView:viewForHeaderInSection: should look as follows:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection (NSInteger)section {
if(section != 2) return nil;
// you can get the title you statically defined in the storyboard
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
CGRect frame = tableView.bounds;
// create and return a custom view
#define LABEL_PADDING 10.0f
HeaderLabelStyling *customLabel = [[HeaderLabelStyling alloc] initWithFrame:CGRectInset(frame, LABEL_PADDING, 0)] ;
UIButton *addButton = [[UIButton alloc] initWithFrame:CGRectMake(frame.size.width-60, 10, 50, 30)];
addButton.backgroundColor = [UIColor whiteColor];
addButton.titleLabel.textColor = [UIColor colorWithRed:240/255.0f green:118/255.0f blue:34/255.0f alpha:1.0f];
addButton.titleLabel.tintColor = [UIColor blackColor];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 30)];
title.text = #"iawn";
customLabel.text = sectionTitle;
customLabel.backgroundColor= [UIColor colorWithRed:143.0f/255.0f green:137.0f/255.0f blue:135.0f/255.0f alpha:1.0f];
customLabel.userInteractionsEnabled = YES;
customLabel.textColor = [UIColor colorWithRed:255/255.0f green:255/255.0f blue:255/255.0f alpha:1.0f];
[customLabel addSubview:addButton];
[addButton addSubview:title];
[addButton addTarget:self action:#selector(receiverButtonClicked:) forControlEvents:UIControlEventTouchDown];
return customLabel;
}

Create custom UITableView header with white text and red background

I have a UITableView that needs a simple custom header that has a background color and a white label. This is my code:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
NSString *titleString = #"Title";
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 45)];
[headerView setBackgroundColor:[UIColor redColor]];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:headerView.frame];
headerLabel.text = titleString;
headerLabel.textColor = [UIColor whiteColor];
[headerView addSubview:headerLabel];
return headerView;
}
I can change the background color without any problem, but for some reason the label always shows gray text color. I'm also using a custom category to change all fonts to a custom one that looks like this (I don't think this is the problem but anyway..):
- (void)awakeFromNib
{
[super awakeFromNib];
[self setFont:[UIFont fontWithName:#"MyFont" size:self.font.pointSize]];
}
-(id)initWithFrame:(CGRect)frame
{
id result = [super initWithFrame:frame];
if (result) {
[self setFont:[UIFont fontWithName:#"MyFont" size:self.font.pointSize]];
}
return result;
}
Any idea why could this be happening?

Is there a way to change the font color of all instances of UITableview's header

The new iOS 7 has changed the default font color of the section headers in tableview. The problem is that my background image makes the text hard to read. I know I could change my background but I would like to change the color of all the textviews. I have changed the uinavigationbar color in my apps delegate before. I would like to use something like that for tableview if possible. I have read this method:
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil) {
return nil;
}else{
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(20, 8, 320, 16);
label.textColor = [UIColor whiteColor];
label.text = sectionTitle;
UIView *view = [[UIView alloc] init];
[view addSubview:label];
return view;
}
My problem with this is that I would have to implement it on a per tableviewcontroller basis. Also when using this I'm not sure how to prevent the text from going off the page and being unreadable.
Any suggestions would be great. Thanks in advance.
EDIT: I have decided to add a little clarification just for show using some suggested code here remains my problem with this solution.
EDIT: To accompany answer. I found that this is also needed to create the space for multiple lines for header.
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section == ?) {
return 60;
}
else{
return 44;
}
}
You must use following method to change your headerview :
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,yourWidth,YourHeight)] ;
headerView.backgroundColor = [UIColor colorWithRed:0.5058f green:0.6118f blue:0.8078f alpha:1.0f];
tableView.sectionHeaderHeight = headerView.frame.size.height;
tableView.tableHeaderView.clipsToBounds = YES;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 13,320, 22)] ;
label.text = [self tableView:tableView titleForHeaderInSection:section];
label.font = [UIFont boldSystemFontOfSize:16.0];
label.shadowOffset = CGSizeMake(0, 1);
label.shadowColor = [UIColor grayColor];
label.backgroundColor = [UIColor clearColor];
// Chnage your title color here
label.textColor = [UIColor whiteColor];
[label sizeToFit];
label.numberOfLines = 2 ;
[headerView addSubview:label];
return headerView;
}

Adding image next to a Title Header in UITableView

I have created a UITableView using Group-style. I only have a single group in my table.
Here are my two questions:
1) How do I display a picture next to my title in the top header? Basically I would like to put the logo of our company next to that title.
2) Can I change the background color (I don't like the grey one) in the header and can I adjust the height of that header?
Just return a custom view to your header this way:
//Draws a custom view for the header instructions
-(UIView*)tableView:(UITableView*) tableView viewForHeaderInSection:(NSInteger)section;
{
if ([tableView numberOfRowsInSection:section] != 0) // To handle nil entries
{
UIView* headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 60)] autorelease];
// set up whatever view you want here. E.g.
UILabel* headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, headerView.frame.size.width - 20.0, headerView.frame.size.height)];
headerLabel.text = #"Tap items in the list to pin them.\nTap the Remix button to clear all unpinned items";
headerLabel.numberOfLines = 0;
headerLabel.font = [UIFont fontWithName:#"HelveticaNeueLTStd-Lt" size: 14];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.textColor = [UIColor colorWithRed:kColorRed green:kColorGreen blue:kColorBlue alpha:1];
headerLabel.userInteractionEnabled = NO;
headerLabel.textAlignment = UITextAlignmentCenter;
headerView.backgroundColor = [UIColor clearColor];
[headerView setTag:010];
[headerView setAlpha:1];
[headerView addSubview:headerLabel];
[headerLabel release];
return headerView;
}
else {
return nil; // again to handle nil tables. You should be nice and at least show an error label on the view.
}
}
And this gives you something like this:

Resources