-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
if (_segment.selectedSegmentIndex==0)
{
return sortedKeys;
}
else
{
return sortedKeys1;
}
}
我使用此代码,但如果名称不存在,我不想要章节标题,现在它给我所有章节标题
答案 0 :(得分:1)
如果没有行,则将该部分的标题设置为nil。
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if ([tableView.dataSource tableView:tableView numberOfRowsInSection:section] == 0)
{
return nil;
} else {
return "section title \(section)"
}
return @"";
}
这样可行。
答案 1 :(得分:1)
尝试在numberOfSectionsInTableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (result_not_found) { /// pass the condition when "result not found" here
return 0;
}
if (_segment.selectedSegmentIndex==0) {
return ([sortedKeys count]);
}
else {
return ([sortedKeys1 count]);
}
}
答案 2 :(得分:0)
你应该使用这个返回正确数量的部分:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
如果相应部分中没有行,则返回CGFLOAT_MIN:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
希望它有所帮助。
汤姆