[英]Why is my first section header in table not showing?
我无法弄清楚为什么第一节标题没有显示。 第二和第三显示罚款。 我怀疑这是因为搜索栏。
我试过偏移整个表,就像UISearchBar所涵盖的节标题,但我没有注意到它偏移。
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
// create the parent view that will hold header Label
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0,-60,300,60)];
// create the label object
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.frame = CGRectMake(0,0,self.view.frame.size.width,60);
UIColor* mainColor = [UIColor colorWithRed:47.0/255 green:168.0/255 blue:228.0/255 alpha:1.0f];
headerLabel.backgroundColor = mainColor;
headerLabel.font = [UIFont boldSystemFontOfSize:18];
headerLabel.textAlignment = UITextAlignmentCenter;
if(section == 0)
headerLabel.text = @"Friends";
if(section == 1)
headerLabel.text = @"Second Section Header";
if(section == 2)
headerLabel.text = @"Third Section Header";
headerLabel.textColor = [UIColor whiteColor];
[customView addSubview:headerLabel];
return customView;
}
- (void) hideSearchBar
{
self.feedTableView.contentOffset = CGPointMake( 0, self.searchBar.frame.size.height );
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//Number of rows it should expect should be based on the section
NSDictionary *dictionary = [_imageDataArray objectAtIndex:section];
NSArray *array = [dictionary objectForKey:@"data"];
return [array count];
}
它没有显示,因为我没有实现heightForHeaderInSection
这里有更多细节: 在iOS 7中,viewForHeaderInSection部分从1开始而不是从0开始
你的代码中有一个错误。 你的标题在那里,但设置在y = -60
,高度为60。
替换:
// create the parent view that will hold header Label
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0,-60,300,60)];
有了这个
// create the parent view that will hold header Label
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,300,60)];
这是这里的屏幕截图
希望有所帮助。
在我的情况下,我需要在代码中显式分配委托,即使委托已经在XIB文件中连接。
tableView.delegate = self
tableView.dataSource = self
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.