Adding image next to a Title Header in UITableView - ios

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:

Related

Adjust frame of UITableView Background View?

I am setting a label inside of an empty table view's background view, which is placing the label in the middle of the tableview.
I'd like to move that label up a bit, so it's near the top of the table view, however the code below isn't working:
UILabel *messageLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, 20)];
messageLbl.text = #"NO REGISTRATIONS FOUND";
messageLbl.font = [UIFont fontWithName:#"Sansation-Bold" size:20.0f];
messageLbl.textColor = [UIColor lightGrayColor];
messageLbl.textAlignment = NSTextAlignmentCenter;
[messageLbl sizeToFit];
//set back to label view
self.tableView.backgroundView = messageLbl;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.backgroundView.frame = CGRectMake(0, 0, self.tableView.bounds.size.width, 100);
Another easy solution is to add your messageLbl to tableHeaderView:
UILabel *messageLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, 50)];
messageLbl.text = #"\n\n NO REGISTRATIONS FOUND";
messageLbl.font = [UIFont fontWithName:#"Sansation-Bold" size:20.0f];
messageLbl.textColor = [UIColor lightGrayColor];
messageLbl.textAlignment = NSTextAlignmentCenter;
messageLbl.numberOfLines = 0;
[messageLbl sizeToFit];
//set back to label view
self.tableView.tableHeaderView = messageLbl;
Quick solution is just by adding \n at the end the of text and set no of lines to 0. Try this
messageLbl.numberOfLines = 0;
messageLbl.text = #"NO REGISTRATIONS FOUND\n\n\n\n";
Looks like you were hiding the Y axis behind Navigation bar
try it by setting some Y axis height & Don't forget to add SubView
UILabel *messageLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, self.tableView.bounds.size.width, 20)];
messageLbl.text = #"NO REGISTRATIONS FOUND";
messageLbl.font = [UIFont fontWithName:#"Sansation-Bold" size:20.0f];
messageLbl.textColor = [UIColor lightGrayColor];
messageLbl.textAlignment = NSTextAlignmentCenter;
[messageLbl sizeToFit];
//set back to label view
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview: messageLbl];

Set position of the header at the beginning of the TableView

I have to set my headerView at the beginning of the table View.
It seems to be fixed please help.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,tableView.frame.size.width,40)] autorelease];
headerView.backgroundColor = [Utility consumerLightColor];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 250, headerView.frame.size.height)];
headerLabel.text = #"Upcoming Appointments";
headerLabel.textAlignment = NSTextAlignmentLeft;
headerLabel.textColor = [UIColor whiteColor];
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
headerLabel.font = [UIFont fontWithName:#"bauhaus md bt" size:15.0f];
}
else{
headerLabel.font = [UIFont fontWithName:#"bauhaus md bt" size:16.0f];
}
// // NSLog(#"title --> %#",[locationArray objectAtIndex:i]);
headerLabel.backgroundColor = [UIColor clearColor];
[headerView addSubview:headerLabel];
[headerLabel release];
return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (tableView == appointmentListtableView) {
CGFloat changeheight = 40.00;
return changeheight;
}
else{
return 0.0f;
}
}
All I want is to set the position of the header view (Upcoming Appointments)at the beginning of the table View and it should not be fixed.
Maybe you need to remove table header , not section header ?
To do this you need to set:
tableView.tableHeaderView = nil
See keep always in mind,
If you want to set Static header to table view then its good & best practice to set it as a ,
yourTableView.tableHeader = yourHeaderView;
If your headers are dynamic you don't know weather how much sections want to set, then go with method,
viewForHeaderInSection:tableView
That doesn't mean you can use only one at a time not at all, You can use both at a time also,
Now comes to your case, i think you are doing something wrong in the tableViewHader, set it to nil if you don't need anymore,
But i am curious about one thing even if you have set tableViewHeader still why does it show upper & lower side of your secion header,You are setting tableViewHeader or sectionFooter or showing the UITableViewCell

How to correct the tableview section header frame according to the orientation / UITableView width

I have a UITableView with grouped style - and I have a custom header view:
+ (UIView *) viewForHeaderWithText: (NSString*) title
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
UIView *viewHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
UILabel *label = [[UILabel alloc] init];
label.backgroundColor = [UIColor clearColor];
label.frame = CGRectMake(10.0f, 0.0f, 300.0f, 20.0f);
label.font = [UIFont systemFontOfSize:12.0f];
label.textColor = [UIColor blackColor];
label.textAlignment = NSTextAlignmentLeft;
label.text = title;
if (!IS_OS_7_OR_LATER) {
label.textColor = [UIColor lightGrayColor];
}
[viewHeader addSubview:label];
return viewHeader;
}
}
So the section header width frame is fixed.
but For different orientation and way of presenting the UITableViewController the cell width (iOS6) is changed - as u can see on the screenshots. But I need to make offset for the section title and cell equal.
Now I have:
What I need:
I tried to change the cell width - but not solve this.. Any help?
You need to impelement this below uitableView delegate method:-
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

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;
}

Setting custom header view in UITableView

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.

Resources