UIScrollView with paging not working ok - iphone

I have implement a scrollview with paging but it not work ok. For every view of the scrollview I must populate a tableview with certain values. Even if the values are correct received all the views are identical (the last view is the view on every subview of scrollview) Where is my mistake?
Here is my code :
- (void)viewDidLoad
{
[super viewDidLoad];
self.vehiculesenvisagesScrollView.pagingEnabled = YES;
self.vehiculesenvisagesScrollView.contentSize = CGSizeMake(vehiculesenvisagesScrollView.frame.size.width * [vehiculesEnvisages count], vehiculesenvisagesScrollView.frame.size.height);
self.vehiculesenvisagesScrollView.showsHorizontalScrollIndicator = NO;
self.vehiculesenvisagesScrollView.showsVerticalScrollIndicator = NO;
self.vehiculesenvisagesScrollView.scrollsToTop = NO;
vehiculesEnvisagesArray = [[NSMutableArray alloc] initWithObjects:#"Gamme", #"Usage",#"Délai d'echart",#"Type d'achart",#"Financement", nil];
for (int i=0; i<[vehiculesEnvisages count];i++){
[self loadScrollViewWithPage:i];
}
NSLog(#"views %#",[vehiculesenvisagesScrollView subviews]);
self.pageControlVehiculeEnvisages.numberOfPages=[vehiculesEnvisages count];
self.pageControlVehiculeEnvisages.currentPage=0;
}
- (void) loadScrollViewWithPage: (int) page {
NSLog(#"%d",page);
if (page < 0) return;
if (page >= [vehiculesEnvisages count]) return;
CGRect frame;
frame.origin.x = self.vehiculesenvisagesScrollView.frame.size.width * page;
frame.origin.y = 0;
frame.size = self.vehiculesenvisagesScrollView.frame.size;
UIView *oneview = [[UIView alloc] initWithFrame:frame];
tableViewVehiculesEnvisages=[[UITableView alloc] initWithFrame:CGRectMake(3, 0, 320, 233) style:UITableViewStyleGrouped];
tableViewVehiculesEnvisages.bounces=NO;
tableViewVehiculesEnvisages.backgroundColor=[UIColor clearColor];
[tableViewVehiculesEnvisages setDelegate:self];
[tableViewVehiculesEnvisages setDataSource:self];
self.gammeString=[[vehiculesEnvisages objectAtIndex:page]valueForKey:#"gamme"];
self.usageString=[[vehiculesEnvisages objectAtIndex:page]valueForKey:#"usage"];
self.delaiString=[[vehiculesEnvisages objectAtIndex:page]valueForKey:#"delai"];
self.typeString=[[vehiculesEnvisages objectAtIndex:page]valueForKey:#"achat"];
self.financementString=[[vehiculesEnvisages objectAtIndex:page]valueForKey:#"financement"];
[oneview addSubview:tableViewVehiculesEnvisages];
NSLog(#"gamme%#", self.gammeString);
[self.vehiculesenvisagesScrollView addSubview:oneview];
[oneview release];
}
where gammeString,usageString,delaiString,typeString,financementString contains the text that will be set to some labels on a cell of tableview.
Here is my cellForRowAtIndexPath method :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellRecherchePartenaires"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"CellRecherchePartenaires"] autorelease];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.backgroundView.opaque = NO;
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.opaque = NO;
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.highlightedTextColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.opaque = NO;
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.highlightedTextColor = [UIColor whiteColor];
cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
if (indexPath.row==0)
{
gammeLabel = [[UILabel alloc] init];
gammeLabel.frame = CGRectMake(150, -4, 150, 50);
gammeLabel.textColor=[UIColor grayColor];
gammeLabel.backgroundColor=[UIColor clearColor];
gammeLabel.text = self.gammeString;
[cell.contentView addSubview: [gammeLabel autorelease]];
}
if(indexPath.row==1)
{
usageLabel = [[UILabel alloc] init];
usageLabel.frame = CGRectMake(150, -4, 150, 50);
usageLabel.textColor=[UIColor grayColor];
usageLabel.backgroundColor=[UIColor clearColor];
usageLabel.text = self.usageString;
[cell.contentView addSubview: [usageLabel autorelease]];
}
if(indexPath.row==2)
{
delaiLabel = [[UILabel alloc] init];
delaiLabel.frame = CGRectMake( 150, -4, 150, 50);
delaiLabel.textColor = [UIColor grayColor];
delaiLabel.backgroundColor = [UIColor clearColor];
delaiLabel.text = self.delaiString;
[cell.contentView addSubview: [delaiLabel autorelease]];
}
if(indexPath.row==3)
{
typeLabel = [[UILabel alloc] init];
typeLabel.frame = CGRectMake( 150, -4, 150, 50);
typeLabel.textColor = [UIColor grayColor];
typeLabel.backgroundColor = [UIColor clearColor];
typeLabel.text =self.typeString;
[cell.contentView addSubview: [typeLabel autorelease]];
}
if(indexPath.row==4)
{
financementLabel = [[UILabel alloc] init];
financementLabel.frame = CGRectMake( 150, -4, 150, 50);
financementLabel.textColor = [UIColor grayColor];
financementLabel.backgroundColor = [UIColor clearColor];
financementLabel.text = self.financementString;
[cell.contentView addSubview: [financementLabel autorelease]];
}
}
[[cell textLabel] setText: [vehiculesEnvisagesArray objectAtIndex:indexPath.row]] ;
return cell;
}

- (void) loadScrollViewWithPage: (int) page {
NSLog(#"%d",page);
if (page < 0) return;
if (page >= [vehiculesEnvisages count]) return;
CGRect frame;
frame.origin.x = self.vehiculesenvisagesScrollView.frame.size.width * page;
frame.origin.y = 0;
frame.size = self.vehiculesenvisagesScrollView.frame.size;
UIView *oneview = [[UIView alloc] initWithFrame:frame];
tableViewVehiculesEnvisages=[[UITableView alloc] initWithFrame:CGRectMake(3, 0, 320, 233) style:UITableViewStyleGrouped];
tableViewVehiculesEnvisages.tag=page;
tableViewVehiculesEnvisages.bounces=NO;
tableViewVehiculesEnvisages.backgroundColor=[UIColor clearColor];
[tableViewVehiculesEnvisages setDelegate:self];
[tableViewVehiculesEnvisages setDataSource:self];
[oneview addSubview:tableViewVehiculesEnvisages];
NSLog(#"gamme%#", self.gammeString);
[self.vehiculesenvisagesScrollView addSubview:oneview];
[oneview release];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellRecherchePartenaires"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"CellRecherchePartenaires"] autorelease];
}
[self setTextt:tableView.tag];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.backgroundView.opaque = NO;
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.opaque = NO;
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.highlightedTextColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.opaque = NO;
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.highlightedTextColor = [UIColor whiteColor];
cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
if (indexPath.row==0)
{
gammeLabel = [[UILabel alloc] init];
gammeLabel.frame = CGRectMake(150, -4, 150, 50);
gammeLabel.textColor=[UIColor grayColor];
gammeLabel.backgroundColor=[UIColor clearColor];
gammeLabel.text = self.gammeString;
[cell.contentView addSubview: [gammeLabel autorelease]];
}
if(indexPath.row==1)
{
usageLabel = [[UILabel alloc] init];
usageLabel.frame = CGRectMake(150, -4, 150, 50);
usageLabel.textColor=[UIColor grayColor];
usageLabel.backgroundColor=[UIColor clearColor];
usageLabel.text = self.usageString;
[cell.contentView addSubview: [usageLabel autorelease]];
}
if(indexPath.row==2)
{
delaiLabel = [[UILabel alloc] init];
delaiLabel.frame = CGRectMake( 150, -4, 150, 50);
delaiLabel.textColor = [UIColor grayColor];
delaiLabel.backgroundColor = [UIColor clearColor];
delaiLabel.text = self.delaiString;
[cell.contentView addSubview: [delaiLabel autorelease]];
}
if(indexPath.row==3)
{
typeLabel = [[UILabel alloc] init];
typeLabel.frame = CGRectMake( 150, -4, 150, 50);
typeLabel.textColor = [UIColor grayColor];
typeLabel.backgroundColor = [UIColor clearColor];
typeLabel.text =self.typeString;
[cell.contentView addSubview: [typeLabel autorelease]];
}
if(indexPath.row==4)
{
financementLabel = [[UILabel alloc] init];
financementLabel.frame = CGRectMake( 150, -4, 150, 50);
financementLabel.textColor = [UIColor grayColor];
financementLabel.backgroundColor = [UIColor clearColor];
financementLabel.text = self.financementString;
[cell.contentView addSubview: [financementLabel autorelease]];
}
[[cell textLabel] setText: [vehiculesEnvisagesArray objectAtIndex:indexPath.row]] ;
return cell;
}
-(void)setTextt:(int)page
{
self.gammeString=[[vehiculesEnvisages objectAtIndex:page]valueForKey:#"gamme"];
self.usageString=[[vehiculesEnvisages objectAtIndex:page]valueForKey:#"usage"];
self.delaiString=[[vehiculesEnvisages objectAtIndex:page]valueForKey:#"delai"];
self.typeString=[[vehiculesEnvisages objectAtIndex:page]valueForKey:#"achat"];
self.financementString=[[vehiculesEnvisages objectAtIndex:page]valueForKey:#"financement"];
}
Replace your code with this code,You defiantly get solution.

I found the solution of my issue : I use a NSMutableArray instead of a NSString and it works ok.

Related

xml data in UITableview in iPhone app

how can i show the xml data in UITableView according to the attribute field of the xml data?
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *) string {
[currentElementValue appendString:string];
}
- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *) elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
NSLog(#"elementName %# %#", elementName, currentElementValue);
currentElementValue=nil;
[currentElementValue release];
}
here is some code where i am getting the element name in my xml file.now i want to display these data on a UITableView cell.
if i become unable to express the problem then please knock....
NOTE THAT: if somebody give me source code then it will better for me,because its very urgent for me.
Thanks in Advance.
Every time you parse your xml data, get that value intoone array and than display that in tableview.
#pragma mark tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
int sectionCount = [records count];
NSLog(#"section cout: %d",sectionCount);
return sectionCount;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:15.0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"arrow.png"]];
cell.accessoryView = imageView;
cell.accessoryType = UITableViewCellSelectionStyleNone;
tableView.separatorColor = [UIColor clearColor];
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
cellView = [[[UIView alloc] initWithFrame:CGRectMake(5,8,290, 120)] autorelease];
cellView.backgroundColor = [UIColor clearColor];
cellView.tag =10;
[cell.contentView addSubview:cellView];
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(2, 40, 48, 48)];
imgView.image = [UIImage imageNamed:#"productbox.png"];
imgView.layer.borderColor = [UIColor blackColor].CGColor;
imgView.layer.borderWidth = 2.0;
imgView.tag = 5;
[cellView addSubview:imgView];
CGRect idLabelRect = CGRectMake(65, 0, 190, 18);
idLabel = [[[UILabel alloc] initWithFrame:idLabelRect] autorelease];
idLabel.textAlignment = UITextAlignmentLeft;
idLabel.textColor = [UIColor blackColor];
idLabel.font = [UIFont systemFontOfSize:12];
idLabel.backgroundColor = [UIColor clearColor];
idLabel.layer.borderColor = [UIColor grayColor].CGColor;
idLabel.tag = 0;
CGRect statusRect = CGRectMake(65, 22, 190, 22);
statusLabel = [[[UILabel alloc] initWithFrame:statusRect] autorelease];
statusLabel.textAlignment = UITextAlignmentLeft;
statusLabel.textColor = [UIColor blackColor];
statusLabel.font = [UIFont systemFontOfSize:12];
statusLabel.backgroundColor = [UIColor clearColor];
statusLabel.layer.borderColor = [UIColor grayColor].CGColor;
statusLabel.tag = 1;
CGRect orderDateRect = CGRectMake(65, 48, 190, 22);
orderDate = [[[UILabel alloc] initWithFrame:orderDateRect] autorelease];
orderDate.textAlignment = UITextAlignmentLeft;
orderDate.textColor = [UIColor blackColor];
orderDate.font = [UIFont systemFontOfSize:12];
orderDate.backgroundColor = [UIColor clearColor];
orderDate.layer.borderColor = [UIColor grayColor].CGColor;
orderDate.tag = 2;
CGRect byRect = CGRectMake(65, 75, 190, 22);
byLabel = [[[UILabel alloc] initWithFrame:byRect] autorelease];
byLabel.textAlignment = UITextAlignmentLeft;
byLabel.textColor = [UIColor blackColor];
byLabel.font = [UIFont systemFontOfSize:12];
byLabel.backgroundColor = [UIColor clearColor];
byLabel.layer.borderColor = [UIColor grayColor].CGColor;
byLabel.tag = 3;
CGRect totalRect = CGRectMake(65, 98, 190, 22);
totalLabel = [[[UILabel alloc] initWithFrame:totalRect] autorelease];
totalLabel.textAlignment = UITextAlignmentLeft;
totalLabel.textColor = [UIColor blackColor];
totalLabel.font = [UIFont systemFontOfSize:12];
totalLabel.backgroundColor = [UIColor clearColor];
totalLabel.layer.borderColor = [UIColor grayColor].CGColor;
totalLabel.tag = 4;
[cellView addSubview:idLabel];
[cellView addSubview:statusLabel];
[cellView addSubview:orderDate];
[cellView addSubview:byLabel];
[cellView addSubview:totalLabel];
}
cellView = (UIView *)[cell.contentView viewWithTag:10];
idLabel = (UILabel *)[cellView viewWithTag:0];
statusLabel = (UILabel *)[cellView viewWithTag:1];
orderDate = (UILabel *)[cellView viewWithTag:2];
byLabel = (UILabel *)[cellView viewWithTag:3];
totalLabel = (UILabel *)[cellView viewWithTag:4];
imgView = (UIImageView *)[cellView viewWithTag:5];
idLabel.text = [NSString stringWithFormat:#"Order Id: %#",[[records objectAtIndex:indexPath.section] objectAtIndex:0]];
statusLabel.text = [NSString stringWithFormat:#"Status: %#",[[records objectAtIndex:indexPath.section] objectAtIndex:1]];
orderDate.text = [NSString stringWithFormat:#"Date: %#",[[records objectAtIndex:indexPath.section] objectAtIndex:2]];
byLabel.text =[NSString stringWithFormat:#"By: %#",[[records objectAtIndex:indexPath.section] objectAtIndex:3]];
totalLabel.text =[NSString stringWithFormat:#"Total: %#",[[records objectAtIndex:indexPath.section] objectAtIndex:4]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//Get the selected country
NSString *selectedSection = [ NSString stringWithFormat:#"%#",[[records objectAtIndex:indexPath.section] objectAtIndex:0] ];
dvController = [[OrderDetailsViewController alloc] initWithNibNameAndCurrentTweetUser:#"OrderDetailsViewController" bundle:nil:selectedSection];
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
dvController = nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 130;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)];
[v setBackgroundColor:[UIColor blackColor]];
UILabel* hdr = [[[UILabel alloc] initWithFrame:CGRectMake(10,0, tableView.bounds.size.width,20)] autorelease];
hdr.textAlignment = UITextAlignmentLeft;
hdr.font = [UIFont fontWithName:#"Arial-BoldMT" size:12];
hdr.textColor = [UIColor whiteColor];
hdr.backgroundColor = [UIColor blackColor];
[v addSubview:hdr];
hdr.text = [NSString stringWithFormat:#"Order #%#",[[records objectAtIndex:section] objectAtIndex:0]];
return v ;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 20;
}

Add textfield and checkbox on tableview

In my Iphone app I have to add textfield at two rows of tableview and a checkbox to one row. Can anyone provide me an example that can help me. I am new on Iphone and I don't know how to start with this.
Thanks in advance..
Hey try this one in your cellForRowAtindexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:#"CellID%d%d",indexPath.section,indexPath.row]];
if (!cell){
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:[NSString stringWithFormat:#"CellID%d%d",indexPath.section,indexPath.row]] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if(indexPath.row == 0){
// NSString *daytime = [[NSUserDefaults standardUserDefaults] objectForKey:#"day"];
UITextField *text0 = [[UITextField alloc] initWithFrame:CGRectMake(20, 12, 120, 22)];
text0.textAlignment = UITextAlignmentLeft;
text0.backgroundColor = [UIColor whiteColor];
text0.borderStyle = UITextBorderStyleNone;
text0.userInteractionEnabled = FALSE;
text0.font = [UIFont boldSystemFontOfSize:17];
text0.textColor = [UIColor blackColor];
text0.delegate = self;
text0.text = #"Type de Parteneria";
[cell addSubview:text0];
UITextField *text1 = [[UITextField alloc] initWithFrame:CGRectMake(120, 12, 160, 20)];
text1.textAlignment = UITextAlignmentRight;
text1.borderStyle = UITextBorderStyleNone;
text1.backgroundColor = [UIColor whiteColor];
text1.userInteractionEnabled = TRUE;
text1.font = [UIFont boldSystemFontOfSize:16];
text1.textColor = [UIColor colorWithRed:114.0f/255.0f green:136.0f/255.0f blue:165.0f/255.0f alpha:1.0];
text1.delegate = self;
[cell addSubview:text1];
}
else if(indexPath.row == 1){
// NSString *daytime = [[NSUserDefaults standardUserDefaults] objectForKey:#"day"];
UITextField *text0 = [[UITextField alloc] initWithFrame:CGRectMake(20, 12, 120, 22)];
text0.textAlignment = UITextAlignmentLeft;
text0.backgroundColor = [UIColor whiteColor];
text0.borderStyle = UITextBorderStyleNone;
text0.userInteractionEnabled = FALSE;
text0.font = [UIFont boldSystemFontOfSize:17];
text0.textColor = [UIColor blackColor];
text0.delegate = self;
text0.text = #"Audi R8";
[cell addSubview:text0];
UIButton *signBtn = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
signBtn.frame = CGRectMake(15, 170, 290, 35);
[signBtn setTitle:#"Sign Up" forState:UIControlStateNormal];
[signBtn setBackgroundImage:[UIImage imageNamed:#"CheckBox.png"] forState:UIControlStateNormal];
[signBtn setFont:[UIFont boldSystemFontOfSize:17]];
[signBtn addTarget:self action:#selector(checkPressed) forControlEvents:UIControlEventTouchDown];
[cell addSubview:signBtn];
}
return cell;
}
If you get problem than message me
as per your comment try this :
if(indexPath.row==1)
{
UITextField *labl=[[UITextField alloc] init];
// where is your text field's frame ????
labl.frame = CGRectMake(10,10,50,50);
labl.text=#"data";
[cell addSubview:labl];
}

how to give different header section title for different sections

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil) {
return nil;
}
UILabel *label = [[[UILabel alloc] init] autorelease];
label.frame = CGRectMake(20, 6, 300, 30);
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor colorWithHue:(136.0/360.0) // Slightly bluish green
saturation:1.0
brightness:0.60
alpha:1.0];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0.0, 1.0);
label.font = [UIFont boldSystemFontOfSize:16];
label.text = sectionTitle;
// Create header view and add label as a subview
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, SectionHeaderHeight)];
[view autorelease];
[view addSubview:label];
return view;
}
this is for one section if i have 9 section then how can i set title
You need to implement
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
delegate method for UITableView.
This might solve your problem:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIControl *containerView = [[[UIControl alloc] initWithFrame:CGRectMake(0, 0, 320, 20)] autorelease];
UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectMake(10, 0, 320, 20)] autorelease];
headerLabel.textColor = [UIColor colorWithRed:50.0/255.0 green:44.0/255.0 blue:37.0/255.0 alpha:1.0];
[headerLabel setFont:[UIFont boldSystemFontOfSize:11.0]];
//[headerLabel setFont:[UIFont boldSystemFontOfSize:13.0]];
headerLabel.backgroundColor = [UIColor clearColor];
if(section == 0)
headerLabel.text = [NSString stringWithFormat:#"First Section"];
if(section == 1)
headerLabel.text = [NSString stringWithFormat:#"Second Section"];
if(section == 2)
headerLabel.text = [NSString stringWithFormat:#"Third Section"];
headerLabel.textColor = [UIColor colorWithRed:78.0/255.0 green:70.0/255.0 blue:59.0/255.0 alpha:1.0];
//headerLabel.font = [UIFont boldSystemFontOfSize:13.0];
[containerView addSubview:headerLabel];
return containerView;
}
Try this!
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *v =[[[UIView alloc] init] autorelease];
v.backgroundColor = [UIColor clearColor];
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 7, tableView.bounds.size.width - 10, 18)] autorelease];
label.textColor = [UIColor whiteColor];
label.font = [UIFont boldSystemFontOfSize:15];
label.backgroundColor = [UIColor clearColor];
if (section == 1) {
label.text = #"Section 1";
[v addSubview:label];
}
else if (section == 2) {
label.text = #"Section 2";
[v addSubview:label];
}
else if (section == 3) {
label.text = #"Section 3";
[v addSubview:label];
}
return v;
}
You need to know the titles, just use an NSArray, make it global alloc and init it in viewDidLoad and release the array in dealloc.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *aView=[[[NSBundle mainBundle] loadNibNamed:#"tblHeader" owner:self options:nil]objectAtIndex:0];
[(UILabel*)[aView viewWithTag:1] setText:[[self.reports objectAtIndex:section] valueForKey:#"domain"]];
return aView;
return nil;
}
Here u can insert your custom cell also u can use the Section and get those section wise you can use switch case and easily access the section.

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;

Potential leak of an object, Build and Analyze, viewForHeaderInSection

I've just mad Build and Analyze with my project on iphone, and i got warning from analyzer which i don't understand, this is my function:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
NSLog(#"user did select row at index: %d.%d", [indexPath section], [indexPath row]);
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
NSLog(#">>> Entering %s <<<", __PRETTY_FUNCTION__);
//UIView *customView;
if(section ==6){
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 36.0)];
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.textColor = [UIColor whiteColor];
headerLabel.highlightedTextColor = [UIColor whiteColor];
headerLabel.shadowColor = [UIColor blackColor];
headerLabel.shadowOffset = CGSizeMake(0, 1);
headerLabel.font = [UIFont boldSystemFontOfSize:13];
headerLabel.frame = CGRectMake(10.0, 0.0, 300.0, 30.0);
headerLabel.text = #"Personal Data";
[customView addSubview:headerLabel];
return customView;
}
if(section ==10){
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 36.0)];
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.textColor = [UIColor whiteColor];
headerLabel.highlightedTextColor = [UIColor whiteColor];
headerLabel.shadowColor = [UIColor blackColor];
headerLabel.shadowOffset = CGSizeMake(0, 1);
headerLabel.font = [UIFont boldSystemFontOfSize:13];
headerLabel.frame = CGRectMake(10.0, 0.0, 300.0, 30.0);
headerLabel.text = #"Actions";
[customView addSubview:headerLabel];
[headerLabel release];
return customView;
}
else {
return nil;
}
NSLog(#"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}
and in the lines with return customView, the analyzer says:
"potential leak of an object allocated in line ###", can somebody explain me why it's hapenning?
You need:
return [customView autorelease];
You allocate your customView and don't release it anywhere in your code. Once you pass it to table view it will get retained so you can safely dispose of its ownership - by sending autorelease message.
P.S. you also forgot to release headerLabel in section == 6 branch