sectionName TableView - What am I doing wrong? - iphone

I'm having some trouble changing the colour and font on a TableView that I have split into 4 sections with names etc., I can;t seem to get it to work I'm not sure what I am doing wrong?
- (NSString *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionName = nil;
switch(section)
{
case 0:
sectionName = [NSString stringWithString:#"Date"];
break;
case 1:
sectionName = [NSString stringWithString:#"Gig"];
break;
case 2:
sectionName = [NSString stringWithString:#"City"];
break;
case 3:
sectionName = [NSString stringWithString:#"Country"];
break;
}
UILabel *sectionHeader = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)] autorelease];
sectionHeader.backgroundColor = [UIColor clearColor];
sectionHeader.font = [UIFont boldSystemFontOfSize:18];
sectionHeader.textColor = [UIColor whiteColor];
sectionHeader.text = sectionName;
return sectionName;
}

you should return the view instead of the string...
This is what you are returning
return sectionName;
And this is what you should return..
return sectionHeader;

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
is the right method for presenting a view in header of UITableView. Here you can return your UILabel object.
You are trying to return a NSString object instead of UILabel. Also the return type of method is wrong. It should be of the type UIView.
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITableViewDelegate_Protocol/Reference/Reference.html

-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section{
NSString *sectionName = nil;
switch(section)
{
case 0:
sectionName = [NSString stringWithString:#"Date"];
break;
case 1:
sectionName = [NSString stringWithString:#"Gig"];
break;
case 2:
sectionName = [NSString stringWithString:#"City"];
break;
case 3:
sectionName = [NSString stringWithString:#"Country"];
break;
}
UILabel *sectionHeader = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)] autorelease];
sectionHeader.backgroundColor = [UIColor clearColor];
sectionHeader.font = [UIFont boldSystemFontOfSize:18];
sectionHeader.textColor = [UIColor whiteColor];
sectionHeader.text = sectionName;
return sectionHeader;
}
The code shows you how to implement viewForHeaderInSection delegate method of TableViewCOntroller which you need to implement to accomplish what you need. your code returns NSString, which is supposed to return UIView.

Related

Hide TableViewHeaderView for a specific TableView section

So I have this Tableview with a few sections, (3) to be exact. I want there to be headers for sections 2 and 3, not for the first sections..
Heres what i've done:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
NSString *sectionName;
UIView *tempView;
tempView = [[UIView alloc]initWithFrame:CGRectMake(0,0,300,20)];
tempView.backgroundColor=[UIColor grayColor];
UILabel *tempLabel = [[UILabel alloc]initWithFrame:CGRectMake(10,0,300,20)];
tempLabel.backgroundColor = [UIColor clearColor];
tempLabel.shadowColor = [UIColor blackColor];
tempLabel.shadowOffset = CGSizeMake(0,2);
tempLabel.textColor = [UIColor whiteColor]; //here you can change the text color of header.
tempLabel.font = [UIFont fontWithName:#"Helvetica" size:14.0f];
switch (section)
{
break;
case 1:
{
sectionName = NSLocalizedString(#"Information", #"Information");
tempLabel.text = sectionName;
[tempView addSubview:tempLabel];
}
break;
case 2:
{
sectionName = NSLocalizedString(#"Tools", #"Tools");
tempLabel.text = sectionName;
[tempView addSubview:tempLabel];
}
break;
}
return tempView;
}
I am confused on what needs to be done... Heres a pic of whats going on :
In the case of section == 0, you are still setting tempView to a new instance of UIView and returning that value, you just aren't setting the title of the label. Also as you learned you should return 0 for tableView:heightForHeaderInSection: for section 0.
So while I was writing this question I cam to a conclusion... but maybe its not the more efficient?
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
CGFloat headerheight = 20.0;
switch (section)
{
case 0: { headerheight = 0.0; } break;
case 1: { headerheight = 20.0; } break;
case 2: { headerheight = 20.0; } break;
}
return headerheight;
}
If anyone has any suggestions on how I don't need to implement this tableview delegate method, please speak up. I feel like I just wouldnt need to return a view for the specified section rather than say a sections header is 0. But I'm not completely sure at the moment, the problem is SOLVED but maybe not solved correctly?
Heres a pic of the result of the solution.

how to set text of titleForHeaderInSection to AlignmentCenter

i used the follow code to set the title at the group tableview title header,but default the text is AlignmentLeft,how to AlignmentCenter?
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if(section==0){
return NSLocalizedString(#"more_titlehead_one", nil);
}else if (section==1){
return NSLocalizedString(#"more_titlehead_two", nil);
}else{
return NSLocalizedString(#"more_titlehead_three", nil);
}
}
Try like this,
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel * sectionHeader = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
sectionHeader.backgroundColor = [UIColor clearColor];
sectionHeader.textAlignment = UITextAlignmentCenter;
sectionHeader.font = [UIFont boldSystemFontOfSize:10];
sectionHeader.textColor = [UIColor whiteColor];
switch(section) {
case 0:sectionHeader.text = #"TITLE ONE"; break;
case 1:sectionHeader.text = #"TITLE TWO"; break;
default:sectionHeader.text = #"TITLE OTHER"; break;
}
return sectionHeader;
}
set some default height for Header,
- (CGFloat)tableView:(UITableView *)tableViewheightForHeaderInSection:(NSInteger)section {
switch(section) {
case 0:
case 1:
default:return 20;
}
}
Use the below method to set the UIView item for the header in each section:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerVw = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 30)] autorelease];
headerVw.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"title_bg.png"]]; // set color of header
UILabel *itemlbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, 320, 20)];
if(section==0){
itemlbl.text = NSLocalizedString(#"more_titlehead_one", nil);
}else if (section==1){
itemlbl.text = NSLocalizedString(#"more_titlehead_two", nil);
}else{
itemlbl.text = NSLocalizedString(#"more_titlehead_three", nil);
}
itemlbl.textAlignment = UITextAlignmentCenter;
itemlbl.backgroundColor = [UIColor clearColor];
itemlbl.textColor = [UIColor whiteColor];
itemlbl.font = [UIFont boldSystemFontOfSize:14];
[headerVw addSubview:itemlbl];
[titlelbl release];
return headerVw;
}
Best of luck
It is very simple use UILabel with its Alignment center. in viewForHeaderInSection
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel *lbl = [[[UILabel alloc] init] autorelease];
lbl.textAlignment = UITextAlignmentCenter;
lbl.backgroundColor=[UIColor clearColor];
lbl.text = #"Header Title";
return lbl;
}

UITableView SectionHeader not displaying custom image background

I'm modifying the look of my TableView's section header. I've managed to get the text working just fine. But the the background image doesn't seem to be showing up at all.
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)] autorelease];
UILabel *sectionTitle = [[[UILabel alloc] initWithFrame:CGRectMake(10, 0, 320, 30)] autorelease];
sectionTitle.text = [[tableDataSource objectAtIndex: section] objectForKey: #"Title"];
sectionTitle.font = [UIFont fontWithName:#"Helvetica-Bold" size:14];
//sectionTitle.textColor = [UIColor whiteColor];
sectionTitle.shadowColor = [UIColor colorWithWhite:0 alpha:0.4];
sectionTitle.shadowOffset = CGSizeMake(1, 1);
sectionTitle.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
//headerView.backgroundColor = [UIColor whiteColor];
UIImageView *sectionHeaderBG = [[UIImageView alloc] init];
UIImage *image = [UIImage imageNamed:#"CellBackgroundGrey4.png"];
sectionHeaderBG.image = image;
[headerView addSubview:sectionTitle];
[headerView addSubview:sectionHeaderBG];
[headerView autorelease];
return headerView;
}
Is there something I'm missing?
Give it a try:
headerView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"CellBackgroundGrey4.png"]];
I think you missed setting the frame of the UIImageView.
//custom sections
- (NSString *)tableView:(UITableView *)tblView titleForHeaderInSection:(NSInteger)section {
NSString *sectionName = nil;
//set the table background to clear so you can see the background view behind it
tableView.backgroundColor = [UIColor clearColor];
//where does this go?
UILabel *sectionHeader = [[UILabel alloc] init];
sectionHeader.backgroundColor = [UIColor clearColor];
sectionHeader.font = [UIFont boldSystemFontOfSize:18];
sectionHeader.textColor = [UIColor whiteColor];
//What is missing here?
switch (section) {
case 0:
sectionName = [NSString stringWithFormat:#"Header Text 1"];
break;
case 1:
sectionName = [NSString stringWithFormat:#"Header Text 2"];
break;
case 2:
sectionName = [NSString stringWithFormat:#"Header Text 3"];
break;
}
return sectionName;

how to add more 2 or more labels in a row of grouped table view

How can I add more than 2 labels into a row of a grouped table view?
I used the following code but it's displaying only the last label
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
switch(indexPath.section)
{
case 0:
switch (indexPath.row)
{
case 0:
{
lbl = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,50, 10)] autorelease];
cell.accessoryView = lbl;
cell.textLabel.text=#"Company:";
lbl.tag = indexPath.row;
UILabel* lbl1 = [[[UILabel alloc] initWithFrame:CGRectMake(0,12,50, 10)] autorelease];
cell.accessoryView = lbl1;
cell.textLabel.text=#"Account:";
lbl1.tag = indexPath.row;
UILabel* lbl2 = [[[UILabel alloc] initWithFrame:CGRectMake(0,24,50, 10)] autorelease];
cell.accessoryView = lbl2;
cell.textLabel.text=#"Other ID:";
lbl2.tag = indexPath.row;
}
break;
case 1:
{ /*
lbl = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,50, 10)] autorelease];
cell.accessoryView = lbl;
cell.textLabel.text=#"Add New Contact:";
*/
}
break;
}
}
To increase the row height, I am using following code:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
switch(indexPath.section)
{
case 0:
{
switch(indexPath.row)
{
case 0:
{
return 100;
}
break;
case 1:
{
return 40;
}
break;
case 2:
{
return 40;
}
break;
case 3:
{
return 40;
}
break;
default:
{
return 15;
}
}
}
break;
}
}}
you can use custom cell and can have anything in your cell, not only two labels, but any thing.....here it goes as far as your problem is concerned......
First thing we will do is create a customCell. Right click on Classes and add a new UITableViewCell subclass. Name as “CustomCell”. Now open CustomCell.h and add the following code:
#interface CustomCell : UITableViewCell {
UILabel *primaryLabel;
UILabel *secondaryLabel;
UIImageView *myImageView;
}
#property(nonatomic,retain)UILabel *primaryLabel;
#property(nonatomic,retain)UILabel *secondaryLabel;
#property(nonatomic,retain)UIImageView *myImageView;
#end
synthesize all the three elements in CustomCell.m as we are going to access these elements from other classes.
#synthesize primaryLabel,secondaryLabel,myImageView;
Open CustomCell.m and add the following code
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {
// Initialization code
primaryLabel = [[UILabel alloc]init];
primaryLabel.textAlignment = UITextAlignmentLeft;
primaryLabel.font = [UIFont systemFontOfSize:14];
secondaryLabel = [[UILabel alloc]init];
secondaryLabel.textAlignment = UITextAlignmentLeft;
secondaryLabel.font = [UIFont systemFontOfSize:8];
myImageView = [[UIImageView alloc]init];
[self.contentView addSubview:primaryLabel];
[self.contentView addSubview:secondaryLabel];
[self.contentView addSubview:myImageView];
}
return self;
}
set the frames in layoutsubviews
- (void)layoutSubviews {
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
CGRect frame;
frame= CGRectMake(boundsX+10 ,0, 50, 50);
myImageView.frame = frame;
frame= CGRectMake(boundsX+70 ,5, 200, 25);
primaryLabel.frame = frame;
frame= CGRectMake(boundsX+70 ,30, 100, 15);
secondaryLabel.frame = frame;
}
now go to tableViewController and just play with your labels
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #”Cell”;
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell…
switch (indexPath.row) {
case 0:
cell.primaryLabel.text = #"Meeting on iPhone Development";
cell.secondaryLabel.text = #"Sat 10:30";
cell.myImageView.image = [UIImage imageNamed:#"meeting_color.png"];
break;
case 1:
cell.primaryLabel.text = #"Call With Client";
cell.secondaryLabel.text = #"Planned";
cell.myImageView.image = [UIImage imageNamed:#"call_color.png"];
break;
case 2:
cell.primaryLabel.text = #"Appointment with Joey";
cell.secondaryLabel.text = #"2 Hours";
cell.myImageView.image = [UIImage imageNamed:#"calendar_color.png"];
break;
case 3:
cell.primaryLabel.text = #"Call With Client";
cell.secondaryLabel.text = #"Planned";
cell.myImageView.image = [UIImage imageNamed:#"call_color.png"];
break;
case 4:
cell.primaryLabel.text = #"Appointment with Joey";
cell.secondaryLabel.text = #"2 Hours";
cell.myImageView.image = [UIImage imageNamed:#"calendar_color.png"];
break;
default:
break;
}
return cell;
}
of course, change the image name//,.......regards

App is crashing as soon as UITableView gets reloaded

I'm developing an app where TableView needs to reload as soon as the login process gets completed. The app crashes with error EXC_BAD_ACCESS when the table data gets reloaded. It doesn't crash when I remove all case instances except case 0:
What could be the reason behind it?
Here's the code:
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(loginDone:)
name:#"loginDone" object:nil];
statTableView.backgroundColor = [UIColor clearColor];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)
section {
return 6;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell.
switch (indexPath.row) {
case 0 :
cell.textLabel.text = #"Foo:";
NSLog(#"%#", data7);
UILabel *myLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 20, 30)];
myLabel2.text = (#"%#", data7);
myLabel2.textColor = [UIColor blackColor];
myLabel2.backgroundColor = [UIColor whiteColor];
myLabel2.font = [UIFont fontWithName:#"Trebuchet MS" size:14];
[cell.contentView addSubview:myLabel2];
break;
case 1:
cell.textLabel.text = #"Foo: ";
UILabel *myLabel4 = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 20, 30)];
myLabel4.text = (#"%#", data11);
myLabel4.textColor = [UIColor blackColor];
myLabel4.backgroundColor = [UIColor whiteColor];
myLabel4.font = [UIFont fontWithName:#"Trebuchet MS" size:14];
[cell.contentView addSubview:myLabel4];
break;
case 2:
cell.textLabel.text = #"Foo: ";
UILabel *myLabel8 = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 20, 30)];
myLabel8.text = (#"%#", data3);
myLabel8.textColor = [UIColor blackColor];
myLabel8.backgroundColor = [UIColor whiteColor];
myLabel8.font = [UIFont fontWithName:#"Trebuchet MS" size:14];
[cell.contentView addSubview:myLabel8];
break;
case 3:
cell.textLabel.text = #"Foo: ";
UILabel *myLabel10 = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 20, 30)];
myLabel10.text = [NSString stringWithFormat:#"%#", data4];
if ([data4 isEqualToString:#"0"]) {
myLabel10.text = #"None";
}
myLabel10.textColor = [UIColor blackColor];
myLabel10.backgroundColor = [UIColor whiteColor];
myLabel10.font = [UIFont fontWithName:#"Trebuchet MS" size:14];
[cell.contentView addSubview:myLabel10];
break;
case 4:
cell.textLabel.text = #"Foo: ";
UILabel *myLabel12 = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 20, 30)];
myLabel12.text = [NSString stringWithFormat:#"%#", data5];
myLabel12.textColor = [UIColor blackColor];
if ([data5 isEqualToString:#"Foo"]) {
myLabel12.textColor = [UIColor redColor];
myLabel12.text = #"Nil";
}
myLabel12.backgroundColor = [UIColor whiteColor];
myLabel12.font = [UIFont fontWithName:#"Trebuchet MS" size:14];
[cell.contentView addSubview:myLabel12];
break;
case 5:
cell.textLabel.text = #"Foo: ";
UILabel *myLabel14 = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 50, 30)];
if ([data6 isEqualToString:#"Foo"]) {
myLabel14.textColor = [UIColor colorWithRed:(0/255.f) green:(100/255.f) blue:(0/255.f) alpha:1.0];
myLabel14.text = #"No Dues";
} else {
myLabel14.text = [NSString stringWithFormat:#"%#", data6];
myLabel14.textColor = [UIColor redColor];
}
myLabel14.backgroundColor = [UIColor whiteColor];
myLabel14.font = [UIFont fontWithName:#"Trebuchet MS" size:14];
[cell.contentView addSubview:myLabel14];
break;
/*
[myLabel2 release];
[myLabel4 release];
[myLabel8 release];
[myLabel10 release];
[myLabel12 release];
[myLabel14 release];
*/
}
return cell;
}
You need to do some basic debugging here. First add break points and go line by line in the debugger until you find the line where the bad exec is happening. Once you have that you will be able to quickly figure out why.
Your codes have some problems.
First, the following method needs an autorelease UITableViewCell object.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
Second, the cell maybe come from [tableView dequeueReusableCellWithIdentifier:CellIdentifier], it already has a custom label which you has added. And you will add more custom label to this kind cell. You can sub-class this kind cell, and obtain the desired UILabel by property.
What are the dataX variables? If they are getting released and not set to nil, you will be calling methods on deallocated objects, which would cause EXC_BAD_ACCESS.