2010-04-11 122 views
10

嗨:我想顯示一個UITableView在表視圖標題(不是部分標題)中的搜索欄的部分索引。 但索引條現在與搜索欄重疊。有沒有一種優雅的解決方案來避免這種情況,並讓索引在表頭下方開始?UITableView部分索引重疊搜索欄

+1

http://stackoverflow.com/questions/556814/changing-the-size-of -uisearchbar-textfield。 。看看邁克的評論 – thndrkiss 2010-04-11 12:39:28

+0

thndrkiss:你的意思是回答。答案不是評論,而且Mike只是在這個問題上發佈了一個答案。 – 2010-04-11 22:26:08

+0

嗨,斯尼你有解決這個問題嗎? – iXcoder 2011-04-02 04:31:33

回答

10

我意識到這個答案來得非常晚,但我有同樣的問題,並在這裏和其他地方搜索。我找到了很好的答案在這裏: Changing the size of the UISearchBar TextField?

有幾個建議,但我發現最簡單的一種是把一個UISearchBar和UINavBar在一個UIView和傳遞以setTableView。問題在於表格控制標題視圖的大小,忽略了您添加的任何設置。因此,添加視圖可以使桌子變得愉快,並且您可以到達並設置搜索欄大小而不受桌面干擾。UINavBar具有與UISearchBar相同的背景樣式,因此可以以視覺上一致的方式填充額外空間你必須調整它的框架 - 見下文:

UISearchBar searchBar = [[[UISearchBar alloc] initWithFrame:CGRectMake(0, 1, 290, 40)] autorelease]; 
UIView *searchView = [[[UIView alloc] initWithFrame:CGRectMake(0, 1, 320, 40)] autorelease]; 
UINavigationBar *navBar = [[[UINavigationBar alloc] initWithFrame:CGRectMake(1, -1, 320, 41)] autorelease]; // same gradient/style as search bar 
[navBar setTintColor:[UIColor lightGrayColor]]; 
[searchView addSubview:navBar]; 
[searchView addSubview:searchBar]; 
+0

喬恩非常感謝你的回答。它爲我工作! :) – alin 2012-11-20 08:51:37

4

我加空字符串數組的索引:

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 
{ 
    NSMutableArray *array = [NSMutableArray arrayWithArray:_sortedKeys]; 

    [array insertObject:@"" atIndex:0]; 
    [array insertObject:@"" atIndex:0]; 

    return array; 
} 
+0

這不會搞亂索引功能嗎? – newenglander 2013-06-25 20:50:38

+0

這對較大的索引沒有幫助,因爲索引將顯示爲圓形「點」而不是一系列索引條目,並且仍與您的視圖重疊。 – RickJansen 2013-07-22 11:18:19