[英]Why is my first section header in table not showing?
I can't figure out why the first section header isn't showing. 我无法弄清楚为什么第一节标题没有显示。 The second and third show fine.
第二和第三显示罚款。 I suspect it's because of the search bar.
我怀疑这是因为搜索栏。
I've tried offsetting the whole table like in UISearchBar covered by section header but I didn't notice it offset. 我试过偏移整个表,就像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];
}
It wasn't showing because i hadn't implemented heightForHeaderInSection 它没有显示,因为我没有实现heightForHeaderInSection
More details here: in iOS 7 viewForHeaderInSection section is starting from 1 not from 0 这里有更多细节: 在iOS 7中,viewForHeaderInSection部分从1开始而不是从0开始
You got a bug in your code. 你的代码中有一个错误。 Your header is there but it is setting at a
y = -60
with a height of 60. 你的标题在那里,但设置在
y = -60
,高度为60。
Replace : 替换:
// create the parent view that will hold header Label
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0,-60,300,60)];
with This 有了这个
// create the parent view that will hold header Label
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,300,60)];
Here is the screen shot here 这是这里的屏幕截图
Hope that helps. 希望有所帮助。
In my case I needed to explicitly assign delegates in code, even though delegates were already connected in the XIB file. 在我的情况下,我需要在代码中显式分配委托,即使委托已经在XIB文件中连接。
tableView.delegate = self
tableView.dataSource = self
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.