2013-04-16 79 views
1

做底層UIViewTable顯示我有一個UIViewController類,它包含一個UITableViewSearchDisplayController。我面臨的問題是,無論何時點擊搜索欄並輸入查詢,它都會顯示搜索結果,並出於某種原因顯示底層的UITableView。爲什麼每當我使用搜索顯示控制器

因此,例如,如果我運行此代碼,而我正在尋找:

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     .. 
     NSLog(@"constructing a search result table view cell ");    
    } else 
    { 
     .. 
     NSLog(@"::: constructing a mail box table view cell"); 
    } 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     NSLog(@"searchDisplayController: number of rows: %d", [queryResultData count]); 
    } else { 
     NSLog(@"mailbox: NUMBER OF ROWS: %d", [self.emailData count]); 
    } 
} 

日誌看起來是這樣:

[5033:c07] performing search with query hi 
[5033:c07] ftSearch started with query = hi dbNum = 0 
[5033:c07] mailbox: NUMBER OF ROWS IN SECTION: 19 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] mailbox: NUMBER OF ROWS IN SECTION: 19 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] searchDisplayController: number of rows are 1 
[5033:c07] ::: constructing a search result table view cell 
[5033:c07] searchDisplayController: number of rows are 2 
[5033:c07] ::: constructing a search result table view cell 
[5033:c07] searchDisplayController: number of rows are 3 
[5033:c07] ::: constructing a search result table view cell 
[5033:c07] searchDisplayController: number of rows are 4 
[5033:c07] ::: constructing a search result table view cell 
[5033:c07] searchDisplayController: number of rows are 5 
[5033:c07] mailbox: NUMBER OF ROWS IN SECTION: 19 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] mailbox: NUMBER OF ROWS IN SECTION: 19 
[5033:c07] searchDisplayController: number of rows are 6 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] mailbox: NUMBER OF ROWS IN SECTION: 19 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] mailbox: NUMBER OF ROWS IN SECTION: 19 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] mailbox: NUMBER OF ROWS IN SECTION: 19 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] mailbox: NUMBER OF ROWS IN SECTION: 19 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] ::: constructing a mail box table view cell 
[5033:c07] ::: constructing a mail box table view cell 

這是什麼IB樣子:

enter image description here

and

enter image description here

要清楚..所顯示的UI正是我所想要的..但​​我只是困惑,爲什麼其他表正在做所有這些渲染計算..這後來成爲了一個真正的問題b/c它給了我一些線程/異步代碼的頭痛,在這個背景和不必要的活動發生的時候真的很難調試。

note:我也嘗試了與IB的不同佈局..我注意到的一件事是,如果我讓搜索欄與tableView相鄰的子視圖,底層表將被調用更多!

我也想過創建一個標誌,並檢查如果搜索欄目前正在使用,然後基本上阻止底層UITAbleView做任何工作..但是這不夠好......它不應該被稱爲首先。

有人能告訴我如何防止底層表出現/正在使用搜索時呈現的所有內容?

回答

1

這並不直接解決我關心的問題..但它有點限制了這種損害。我創建了一個局部變量,如果搜索是積極的,用於存儲:

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller { 
    isSearchViewDisplayed = YES; 
} 

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller { 
    isSearchViewDisplayed = NO; 
} 

我注意到在進一步inspection的第一件事就是實現代碼如下要求其UITableViewDataSource是區段的數..一切(即每節的行數,和細胞的渲染)後發生的..所以我只是在部分返回0檢查部分:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    if (tableView == self.searchDisplayController.searchResultsTableView 
      || !isSearchViewDisplayed) { 
     return 1; 
    } else { 
     return 0; 
    } 
} 

我不會記住這是正確的..只是等待,看看是否有更好的回答在那裏。

相關問題