2011-08-15 80 views
-1

我有一个UISearchBar,当我搜索内容时,它只显示第一部分的标题。我有3个部分,这个代码:搜索时的UISearchBar和标题标题

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 

    if(searching) 
     return @"Search Results"; 

    if (section == 0) 
     return @"First"; 
     if(section == 1) 
      return @"Second"); 
     else 
      return @"Third"); 
} 

是否应该当我搜索标题应根据节的内容而改变?

I.E.如果我的“第一”中的“A”,“第二”中的“B”,“第三”中的“C”,它是否应该更改为我搜索的每个元素的相应部分标题?另外,当我从第一部分(即“C”)搜索到不同的内容时,我点击它的行,nib控制器不显示出来。它恰好显示了第一部分的元素。为什么?

回答

2

首先,我们不知道搜索变量的价值是多少。它的失败,因为它给你的FALSE值。这是我简单的解决方案。你并不需要检查是否是搜索模式或没有,你可以随时返回默认的字符串,我们假设是@“搜索结果”

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 

NSString *headerTitle; 
    if (section == 0) 
     headerTitle = @"First"; 
    if(section == 1) 
     headerTitle = @"Second"); 
    else if(section == 2) 
     headerTitle = @"Third"); 
    else 
     headerTitle = @"Search Results"; 
return headerTitle; 
}