UITableView中的节标题视图重复

Posted

技术标签:

【中文标题】UITableView中的节标题视图重复【英文标题】:Section headerview in UITableView Duplicated 【发布时间】:2009-12-30 14:16:49 【问题描述】:

我正在创建一个应用程序,并且我有一个自定义的 tableview 标题。但有时标题会重复。它取代了 tableview 中的一行。我该如何解决这个奇怪的问题?顺便说一句,我的 tableviewstyle 很简单,页眉高度为 44.0,页脚高度为 0.0。这是它如何显示的图像。标题“cmets”在标题“Messages”下方重复,而它应该是行。

这里是这个视图的完整实现

// 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];


【问题讨论】:

代码中的哪个类显示评论视图?注释应该是表格行单元格还是应该是标题视图? 这是一个标题视图。但有时它会自动变成行。我不知道我发布完整代码的原因 【参考方案1】:

尝试为您的空白部分设置一个高度为零的行。这似乎使他们正确地为我绘制。

【讨论】:

【参考方案2】:

我认为有几个解释:

(1) 您正在显示一个空部分的部分标题,这将导致两个标题一个在另一个之上出现,中间没有行。

(2) 您在– tableView:cellForRowAtIndexPath: 中将标题视图作为行单元格返回,因为标题“它取代了 tableview 中的一行”我认为这是最可能的解释。检查– tableView:viewForHeaderInSection:

【讨论】:

以上是关于UITableView中的节标题视图重复的主要内容,如果未能解决你的问题,请参考以下文章

更改 tableview 的 inset 时 UITableView 中的节标题

NSInternalInconsistencyException 表格视图中的节号错误?

IOS表视图中的节数动态

静态 UITableView 中的节标题大小不均匀

使用标头数组更改UITableView中的节标题背景颜色

UITableView 中的自定义绘图在 5 个单元格后失败