iOS 使用UILocalizedIndexedCollation实现通讯录样式的索引

来源:互联网 发布:unity3d 对象数组 编辑:程序博客网 时间:2024/04/30 18:36

前一段时间要在应用程序中要加入检索的功能,在网上找了很多是手写的,于是今天在这里写一下备忘。直接贴的项目代码。

第一步:对数据源进行排序和分区处理。这里addressBookArray是存储最后的排序完数组。backToControllerData是函数传入的数据源。
NSMutableArray*addressBookArrayTmp=[[NSMutableArrayalloc]init];
            addressBookArrayTmp = (NSMutableArray*)backToControllerData;//将数据传给临时数组,对临时数组进行排序
           
//*************增加排序和索引**************8*
           
addressBookArray=[NSMutableArrayarrayWithCapacity:1];
           
UILocalizedIndexedCollation *indexedCollation=[UILocalizedIndexedCollationcurrentCollation];
           
for (ISSTUserModel*theAdressBookModel in addressBookArrayTmp) {
               
NSInteger section=[indexedCollationsectionForObject:theAdressBookModelcollationStringSelector:@selector(name)];
                theAdressBookModel.
section=section;//这里的addressModel是一个类,里面有name属性,这个方法是按照name来分区。
            }
           
NSInteger sectionCount=[[indexedCollationsectionTitles]count];
           
NSMutableArray *sectionsArray=[NSMutableArrayarrayWithCapacity:sectionCount];//这里使用的是二维数组,这是外面一层
           
for (inti=0; i<=sectionCount; ++i) {
               
NSMutableArray *singleSectionArray=[NSMutableArrayarrayWithCapacity:1];//这是在每个外层数组里面初始化子数组
                [sectionsArray
addObject:singleSectionArray];
            }
           
for(ISSTUserModel*theAdressBookModel in addressBookArrayTmp){//将对象放到正确的区段当中
                [(
NSMutableArray*) [sectionsArray objectAtIndex:theAdressBookModel.section]addObject:theAdressBookModel];
            }
           
           
for (NSMutableArray*singleSectionArray in sectionsArray){//排序子数组中的数据
               
NSArray *sortedSection=[indexedCollationsortedArrayFromArray:singleSectionArraycollationStringSelector:@selector(name)];
                [
addressBookArrayaddObject:sortedSection];
            }
           
            //*********增加排序和索引**********

第二步:实现tableview的字母索引以及索引与分区的对应。这里使用 UILocalizedIndexedCollation 很方便的实现这样的对应。

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
   
// Return the number of sections.
   
return [addressBookArraycount];
}
-(
NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
   
if(tableView==self.searchController.searchResultsTableView)
       
return [searchArraycount];
   
else
       
return [[addressBookArrayobjectAtIndex:section]count];
   
//   return studyArray.count;
}

-(
UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
   
UITableViewCell *cell=(UITableViewCell*)[tableView  dequeueReusableCellWithIdentifier:CellIdentifier];
   
if (cell == nil) {
        cell = [[
UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:CellIdentifier];
    }
  
if(tableView==self.searchController.searchResultsTableView)
       cell.
textLabel.text=[searchArrayobjectAtIndex:indexPath.row];
  
else{
      
ISSTUserModel *adressBook=[[addressBookArrayobjectAtIndex:[indexPathsection]]objectAtIndex:[indexPathrow]];
       cell.
textLabel.text=  adressBook.name;//[namesArray objectAtIndex:indexPath.row];
   }
    cell.
accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
   
return cell;
}
//这里是实现分区命名。
-(NSString*) tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section{
   
if ([[addressBookArrayobjectAtIndex:section]count] >0) {
       
return [[[UILocalizedIndexedCollationcurrentCollation]sectionTitles]objectAtIndex:section];
    }
   
return nil;
}
//这里是在右边的索引项
-(NSArray*) sectionIndexTitlesForTableView:(UITableView*)tableView{
   
return  [[UILocalizedIndexedCollationcurrentCollation]sectionIndexTitles];
}
//这里处理的时索引项与分区的对应
-(NSInteger) tableView:(UITableView*)tableView sectionForSectionIndexTitle:(NSString*)title atIndex:(NSInteger)index  {
   
return [[UILocalizedIndexedCollationcurrentCollation]sectionForSectionIndexTitleAtIndex:index];
}

这里要注意搜索的时候要将我们整理好的数组扁平化处理。
我们要首先要申请三个对象,

@property (retain,nonatomic) NSArray *filteredProducts;

@property (strong,nonatomic) UISearchBar* searchBar;

@property (strong,nonatomic) UISearchDisplayController* searchController;


// Create search bar

    self.searchBar = [[UISearchBaralloc] initWithFrame:

                       CGRectMake(0.0f,0.0f, 320.0f,44.0f)];

    self.tableView.tableHeaderView =self.searchBar;


初始化搜索框和搜索控制器。

    // Create and configure the search controller

    self.searchController = [[UISearchDisplayControlleralloc]

                              initWithSearchBar:self.searchBar

                              contentsController:self];

    

    self.searchController.searchResultsDataSource =self;

    self.searchController.searchResultsDelegate =self;


//这里是初始化将二维的数组扁平成普通数组。

NSMutableArray *flattenedArray = [[NSMutableArrayalloc]

                                      initWithCapacity:1];

    for (NSMutableArray *theArrayin self.products)

    {

        for (int i=0; i<[theArraycount];i++)

        {

            [flattenedArray addObject:[theArrayobjectAtIndex:i]];

        }

    }

    //构造谓词进行筛选,赋值给filteredProduct,那么接下来只要在搜索框的tableview里显示数据就可以了。

    // Set up an NSPredicate to filter the rows

    NSPredicate *predicate = [NSPredicatepredicateWithFormat:

                              @"name beginswith[c] %@",self.searchBar.text];

    self.filteredProducts = [flattenedArray

                             filteredArrayUsingPredicate:predicate];




















0 0
原创粉丝点击