3

I'm trying to mimic the behaviour of a table view similar to the iPod app for Artists - it's a sectioned table view with a section index on the right, with a search bar at the top, but initially hidden when view shown.

I am using sdk 3.1.2 and IB, so simply dragged a UISearchDisplayController into my NIB - it wires everything up for searching. The problem starts because I'm adding the UISearchBar to the first section of the UITableView, because if I understand correctly I must do this so I can jump to the search bar by touching the search icon in the section index directly? When the table view appears I see the search bar but it has resized and I now have a white block behind the section index at the top. It doesn't take the color of the UISearchBar's surround which interestingly is different to that shown in Interface Builder.

I found a tip to add a small navigation bar and a UISearchBar in a UIView, then add this to the table view cell. This works, but the color of the navigation bar's background is what you'd expect normally (gray), not the different color as noted above. More interesting, if I tap the search bar to start a search, then tap Cancel, all is fixed. The background along the whole tableview cell when the search bar is, is the same?

4

2 回答 2

12

回答了我自己的问题,但可能对其他初学者有帮助:

  • 视图加载时将搜索栏放在表格视图标题中,将 contentOffset 向下滚动到此搜索栏的高度(通常为 44 像素),但易于动态检查
  • 使用“{search}”在部分索引中添加搜索图标
  • 为搜索设置 sectionIndexSection -1
  • 处理部分索引触摸时,使用搜索“-1”的“特殊”索引值(将 contentOffset 滚动回 (0, 0)

更新:使用字符串常量UITableViewIndexSearch字符串代替未记录的字符串“{search}” - 即在实现时将其作为数组项之一返回:

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

UITableViewDataSource中。

于 2009-11-25T10:13:40.010 回答
1

将搜索控制器放在 tableView 的开头并尝试以下操作:

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    if (index==0){
        [tableView scrollRectToVisible:CGRectMake(0,0,1,1) animated:NO];
        return -1;
    }
...
}


- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
        NSMutableArray * retValue =[NSMutableArray arrayWithArray:[YOUR ARRAY]];
        [retValue insertObject:UITableViewIndexSearch atIndex:0];
       return retValue;
 } 
于 2012-06-25T11:28:11.540 回答