2010-09-15 153 views
1

我實現一個表的索引視圖,並驚訝地看到我的表索引工作,即使沒有實現:節索引表視圖

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index method. 

我只執行:

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

奇怪的是,當我在斷點運行時,一旦我點擊任何索引值,我的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

方法被調用。

任何線索爲什麼會這樣發生,然後sectionForSectionIndexTitle方法的意義是什麼。

回答

1

,如果你有字母表中的所有字母的列表,列表僅包含了一些項目,你可以使用下面的代碼:

//問數據源返回具有給定標題的部分指標和章節標題索引。 - (NSInteger的)的tableView:(UITableView的*)的tableView sectionForSectionIndexTitle:(的NSString *)標題atIndex:(NSInteger的)指數{

if (tableView == self.searchDisplayController.searchResultsTableView || self.searchBar.text.length > 0) 
{ 
    return 0; 
} 
else 
{ 

    //direct - firsttime match 
    if([self.realMIndexArray containsObject:title]) { 
     NSInteger count = 0; 
     for(NSString *character in self.realMIndexArray) 
     { 
      if([character isEqualToString:title]){ 
       return count; 
      } 
      count ++; 
     } 
    } 
    else { 

     //take next higher letter from alphabet and check if its contained in the "available letters list" 
     //if not, select last entry of list 
     for(int i = [self.indexArray indexOfObject:title] + 1; i < [self.indexArray count]; i++) { 

      NSString* character = [self.indexArray objectAtIndex:i]; 

      if([self.realMIndexArray containsObject:character]) { 

       return [self.realMIndexArray indexOfObject:character]; 

      } 

     } 

     return [self.realMIndexArray count] - 1; 

    } 
    return 0;// in case of some eror donot crash d application 

} 

}

realMIndexArray計數==字母列表確實存在 indexArray = alphbeth中所有字母的列表。

希望這可以幫助別人(把我的一點點時間來弄明白)

0

任何線索爲什麼會這樣發生

是。當你在表格索引上點擊(你不點擊一個iPhone)時,底層表格視圖將會跳轉到該部分以及該部分中的單元格。爲了做到這一點,它必須要求這些單元格的數據源,以便它可以在屏幕上呈現它們。

是什麼 sectionForSectionIndexTitle方法 的意義呢。

tableView:sectionForSectionIndexTitle:atIndex:的文檔(它是在UITableViewDataSource協議的可選方法)表示:

詢問數據源返回 索引具有所述給定 標題和章節的標題索引的部分的。

你只用一節索引 表視圖實現此方法列表,這隻能是表視圖中的普通樣式 (UITableViewStylePlain)創建 。

這是否適用於您的UITableView?換句話說,你使用分組表格視圖樣式嗎?

+0

我明白你的第一個點,但怎麼就對指數點擊帶我到正確的部分。是的,我正在實施UITableViewStylePlain。 – Abhinav 2010-09-15 23:24:53

+0

你剛纔問我「爲什麼在索引上點擊(不是」點擊「)會把用戶帶到正確的部分?」...... *因爲這就是它應該做的*。 (我想你不打算問我這個問題,請換一下。) – 2010-09-15 23:37:04

+0

你說得對。我相信這個行爲是通過tableView:sectionForSectionIndexTitle:atIndex:方法實現的,它將部分索引與實際部分進行映射。那麼如果沒有實現這個方法而不讓框架知道哪個節索引指向哪個節,我會得到期望的行爲? – Abhinav 2010-09-16 18:47:37