如果在ios8中使用搜索栏找不到结果,如何隐藏节标题

时间:2015-10-14 08:15:57

标签: ios objective-c iphone searchbar

IF Name is present then it will look like this

IF Name is not present then it look like this i want to hide section title if name is not present

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    if (_segment.selectedSegmentIndex==0)
    {
        return sortedKeys;
    }
    else
    {
       return sortedKeys1;
    }
}

我使用此代码,但如果名称不存在,我不想要章节标题,现在它给我所有章节标题

3 个答案:

答案 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

中返回0
- (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;

希望它有所帮助。

汤姆