2010-09-15 154 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