【问题标题】:Index like iOS contact appiOS 联系人应用程序之类的索引
【发布时间】:2013-07-16 10:24:24
【问题描述】:

我想创建一个类似iOS 联系人应用程序的视图,

基本上我想要的是位于右侧的垂直ABCDEFG.... 这张图片的一侧,当用户点击字符M我想 获取字符M 我应该如何实现这一点?有没有iOS 控件给了我这个 `ABCDEF...?

提前致谢。

编辑

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return [[NSArray arrayWithObject:UITableViewIndexSearch] arrayByAddingObjectsFromArray:
            [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles]];
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    if (title == UITableViewIndexSearch)
    {
        CGRect searchBarFrame = self.searchDisplayController.searchBar.frame;
        [tableView scrollRectToVisible:searchBarFrame animated:YES];

        return -1;
    }
    else {
        UILocalizedIndexedCollation *currentCollation = [UILocalizedIndexedCollation currentCollation];
        NSLog(@"selected charecter=%ld",(long)[currentCollation sectionForSectionIndexTitleAtIndex:index]);
        return [currentCollation sectionForSectionIndexTitleAtIndex:index-1];
    }
}

【问题讨论】:

  • 你想创建一个带有索引的自定义 UITableView 还是只想展示内置的联系人选择器?
  • 我不想创建自定义表格视图,我只想要位于右侧的 ABCDEF...。该控件的名称是什么?是联系人选择器吗??
  • 你有一个表格视图,里面有你自己的数据吗?或者您正在使用联系人选择器?请说明您想做什么,否则我们不知道。您所指的控件是UITableView 的一部分,如果您没有UITableView,那么如果不自己从头开始编写整个内容,您将无法获得它。
  • 我只想要突出显示的部分ABCD... 我有 tablview 但它是自定义的 tableview 而不是普通的普通 tableview。是否可以使用自定义 tableview 创建相同的视图?
  • 所以,当我问“你想创建一个带有索引的自定义 UITableView”并且你说“不”时,你的实际意思是“是”。谷歌搜索“UITableView index”有数百个例子。

标签: iphone ios ipad contacts


【解决方案1】:

你需要实现这两个方法:

// return list of section titles to display in section index view (e.g. "ABCD...Z#")
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;                                                    
// tell table which section corresponds to section title/index (e.g. "B",1))
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index;

对应的数据源:

例如,如果你有一个数组

`@"Cat", @"Mouse", @"Dog", @"Horse", @"Duck", @"Monkey"`

那么您将返回并为sectionIndexTitlesForTableView 排列为:

`@[@"C", @"D", @"H", @"M"]`

因为@"Dog"@"Duck" 对应@"D"@"Mouse"@"Monkey" 对应@"M" 等等。

当然,对于sectionForSectionIndexTitle,您返回该索引的相应索引。 (1 代表 @"D",3 代表 @"M" 等)

【讨论】:

  • 我想要完整的ABCD... 我必须这样写?? - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return[NSArray arrayWithObjects:@"A", @"B", @"C", @"D", @"D",.... nil]; } 以及如何获取选定的字符??
  • 是的,没错。要获取第一个字符,您可以自己完成(这并不难),或者如果您已经拥有 NSArrayNSFetchedResultsController,您可以使用 NSPredicate 获取它们,尽管我不确定对于NSArray 它是如何完成的,因为我不喜欢这种方法。我喜欢自己做,有更多的控制权。
  • 我试过这个,我已经满了ABCDE...,现在当我点击第一个字符的搜索图标时,搜索栏没有出现....见我的编辑 在问题中
  • 好吧,搜索栏需要有自己的部分(并且也是索引数组中的第一项),而不是作为标题视图添加或简单地附加到表格视图的滚动视图。但如果你这样做,我认为你需要使用[tableView scrollRectToVisible:CGRectMake(0, -44, 320, 1) animated:YES]; 或类似的东西。
  • 我没有在第一个单元格中添加serachBar,我需要添加吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多