views:

505

answers:

2

I am creating an application and I have a customized header of tableview. But sometimes the header gets duplicated. It takes the place of a row in tableview. How do I solve this strange problem? BTW, my tableviewstyle is plain, Header height is 44.0 and the footer height is 0.0. Here is an image how it displays. The header "comments" is duplicated just below the header "Messages" while it should be the row.

alt text

Here is the complete implementation of this view

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
TitleForSectionView=[[NSArray arrayWithObjects:@"Dates To Remember",@"Messages",@"Comments",@"Wishlist",@"Reminders",@"Bookmarks",nil] retain];
self.MySectionIndexArray=[[NSMutableArray alloc] init];
[self.MySectionIndexArray addObject:@"UP"];
[self.MySectionIndexArray addObject:@"DOWN"];
[self.MySectionIndexArray addObject:@"DOWN"];
[self.MySectionIndexArray addObject:@"DOWN"];
[self.MySectionIndexArray addObject:@"DOWN"];
[self.MySectionIndexArray addObject:@"DOWN"];
self.IconsForSectionsView=[[NSMutableArray alloc] init];
[self.IconsForSectionsView addObject:[UIImage imageNamed:@"IconCalender.png"]];
[self.IconsForSectionsView addObject:[UIImage imageNamed:@"IconMessage.png"]];
[self.IconsForSectionsView addObject:[UIImage imageNamed:@"IconComments.png"]];
[self.IconsForSectionsView addObject:[UIImage imageNamed:@"IconWishList.png"]];
[self.IconsForSectionsView addObject:[UIImage imageNamed:@"IconReminder.png"]];
[self.IconsForSectionsView addObject:[UIImage imageNamed:@"IconBookMark.png"]]; 
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {     
return 6;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 if ([[self.MySectionIndexArray objectAtIndex:section] isEqualToString:@"UP"]) {
     return 4;
 } else {
 return 0;
 }
}   


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *MyDashBoardCell=nil;
static NSString *AddNewDateCellIdentifier=@"AddNewDateCell";
static NSString *CellIdentifier = @"DashBoardCell";

DashBoardCustomCellObject = (DashBoardCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (DashBoardCustomCellObject == nil) {
    [[[[NSBundle mainBundle] loadNibNamed:@"DashBoardCustomCell" owner:self options:nil] retain]autorelease];
}
[DashBoardCustomCellObject SetDashBoardCellData:@"Mar 9" EventText:@"My Birthday"];

AddNewDateCellObject = (AddNewDateCell *)[tableView dequeueReusableCellWithIdentifier:AddNewDateCellIdentifier];
if (AddNewDateCellObject == nil) {
    [[[[NSBundle mainBundle] loadNibNamed:@"AddNewDateCell" owner:self options:nil] retain]autorelease];
}
if(indexPath.section==0 && indexPath.row==0)
{
        MyDashBoardCell=AddNewDateCellObject;
}
else
{
    MyDashBoardCell=DashBoardCustomCellObject;
}
return  MyDashBoardCell;
}



- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

// create the parent view that will hold header Label and button and icon image
self.customView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,tableView.bounds.size.width,44)] autorelease];
self.customView.backgroundColor=[UIColor whiteColor];
// create image object
UIImageView *icon= [[[UIImageView alloc] initWithImage:[self.IconsForSectionsView objectAtIndex:section]] autorelease];
icon.contentMode=UIViewContentModeCenter;
icon.frame = CGRectMake(0,0,40,40);

// create the label objects
UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.font = [UIFont boldSystemFontOfSize:13];
headerLabel.frame = CGRectMake(53,11,172,21);
headerLabel.text =  [TitleForSectionView objectAtIndex:section];
headerLabel.textColor = [UIColor blackColor];
headerLabel.highlightedTextColor=[UIColor whiteColor];

    // create the button objects
ButtonDrop = [[[UIButton alloc] initWithFrame:CGRectMake(278, 9, 25, 25)] autorelease];
ButtonDrop.tag=section;
if ([[self.MySectionIndexArray objectAtIndex:section] isEqualToString:@"UP"])
{
    [ButtonDrop setBackgroundImage:[UIImage imageNamed:@"Up.png"] forState:UIControlStateNormal];
    self.customView.backgroundColor=[UIColor blueColor];
    headerLabel.highlighted=YES;
}
else
{
        [ButtonDrop setBackgroundImage:[UIImage imageNamed:@"Down.png"] forState:UIControlStateNormal];
}
[ButtonDrop addTarget:self action:@selector(checkAction:) forControlEvents:UIControlEventTouchUpInside];
[self.customView addSubview:icon];
[self.customView addSubview:headerLabel];
[self.customView addSubview:ButtonDrop];
return self.customView; 
}


- (void)checkAction:(id)sender
{
UIButton *Button = (UIButton*)sender;
NSLog(@"Button Tag=%d",Button.tag);
if([[self.MySectionIndexArray objectAtIndex:Button.tag] isEqualToString:@"UP"])
{
    [self.MySectionIndexArray replaceObjectAtIndex:Button.tag withObject:@"DOWN"];
    [ButtonDrop setBackgroundImage:[UIImage imageNamed:@"Up.png"] forState:UIControlStateNormal];
}
else
{
    [self.MySectionIndexArray replaceObjectAtIndex:Button.tag withObject:@"UP"];
    [ButtonDrop setBackgroundImage:[UIImage imageNamed:@"Down.png"] forState:UIControlStateNormal];
}
[TableDashBoard reloadSections:[NSIndexSet indexSetWithIndex:Button.tag] withRowAnimation:UITableViewRowAnimationFade];

}
A: 

I think there are a couple of explainations:

(1) You're displaying the section header of an empty section which would cause two headers to appear one on top of the other with no rows in between.

(2) You are returning a header view as a row cell in – tableView:cellForRowAtIndexPath: Since the header "it takes place of a row in tableview" I think this the most likely explanation. Check – tableView:viewForHeaderInSection: as well.

TechZen
i am posting my code see if it helps
Rahul Vyas
A: 

Try having a zero-height row for your empty sections. That seems to make them draw correctly for me.

Rowan Christmas