iOS - 高仿通讯录之商品索引排序搜索

项目中像一些商品搜索类界面, TableView添加右侧索引的使用越来越多, 的确用户体验提高了许多.

大致思路:

  1. 添加并设置右侧索引
  2. 自定义汉字转化成拼音文件,通过拼音去匹配首字母
  3. 将库存数据按照索引分组排序
  4. 添加搜索功能
  5. 搜索界面复用库存界面, 增加标识判断显示界面

一. 添加并设置右侧索引

设置右侧索引数组:

// 索引标题数组
@property (nonatomic, strong) NSMutableArray * indexArr;

 // 设置右侧索引数组
 _indexArr = [[NSMutableArray alloc]init];
for(char c = 'A'; c<='Z'; c++) {
     [_indexArr addObject:[NSString stringWithFormat:@"%c", c]];
 }
 [_indexArr addObject:[NSString stringWithFormat:@"#"]]; // 

设置右侧索引字体颜色:

 self.tableView.sectionIndexColor = [UIColor grayColor];

设置右侧索引背景色:

self.tableView.sectionIndexBackgroundColor = [UIColor whiteColor]; 

设置标签数(其实就是分区数目):

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return indexArr.count;
}

显示每组标题索引 (如果不实现 就不显示右侧索引):

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    
    return indexArr;
}

设置索引section的高度:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}

返回每个索引的内容:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    return indexArr[section];
}

这时候基本的索引展现了, 需要响应点击索引则添加下面方法 及 将数据分类展现.
响应点击索引时的委托方法(点击右侧索引表项时调用):

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
      NSInteger count = 0;
    for(NSString *character in _toBeReturned) {
        if([character isEqualToString:title]) {
            return count;
        }
        count ++;
    }
    return 0;
}

二. 自定义汉字转化成拼音文件,通过拼音去匹配首字母

将数据分类展现则是一个大问题!
因为后台返回给我们是所有数据,没法通过分区去控制各个分区数据的数据, 这个时候就考虑到汉字转化成拼音,然后通过拼音去匹配首字母然后排序解决这个问题.

封装一个汉字转化成拼音YYPChineseToPinyin 文件:
1.根据汉字返回汉字的拼音:

/**
 *  根据汉字返回汉字的拼音
 *
 *  @param word 一个汉字
 *
 *  @return 拼音的字符串
 */
+ (NSString *)transformChinese:(NSString *)word {
    NSMutableString *pinyin = [word mutableCopy];
    CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformMandarinLatin, NO);
    CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformStripCombiningMarks, NO);
    
    return [pinyin uppercaseString];
}

2.比较对象数组:

/**
 *  排序后的首字母(不重复)用于tableView的右侧索引
 *
 *  @param objectArray  需要排序的对象数组
 *  @param key          需要比较的对象的字段
 *
 *  @return 排序后的首字母(不重复)
 */
+ (NSMutableArray *)indexWithArray:(NSArray *)objectArray Key:(NSString *)key;

/**
 *  给对象数组排序
 *
 *  @param objectArray 需要排序的对象数组
 *  @param key       需要比较的对象的字段
 *
 *  @return 根据字段排序后的对象数组(同首写字母的在一个数组中)
 */
+ (NSMutableArray *)sortObjectArray:(NSArray *)objectArray Key:(NSString *)key;

3.比较字符串数组:

/**
 *  排序后的首字母(不重复)用于tableView的右侧索引
 *
 *  @param stringArr 需要排序的字符数组
 *
 *  @return 排序后的首字母(不重复)
 */
+ (NSMutableArray*)indexArray:(NSArray*)stringArr;

/**
 *  返回名称
 *
 *  @param stringArr 需要排序的字符数组
 *
 *  @return 更具首字母排序后的字符数组
 */
+ (NSMutableArray *)letterSortArray:(NSArray *)stringArr;

三. 将库存数据按照索引分组排序

解决匹配首字母问题后, 则需要在所需控制器内去解决分组排序问题了

// 库存详情列表数组
@property (nonatomic, strong) NSMutableArray *inventoryList;

// 索引标题数组(排序后的出现过的拼音首字母数组)
@property(nonatomic, strong) NSMutableArray *indexArr;

// 排序好的结果数组
@property(nonatomic, strong) NSMutableArray *resultArr;
    // 根据YYPGoodsModel对象的 name 属性 按中文 对 YYPGoodsModel数组 排序
    self.indexArr = [YYPChineseToPinyin indexWithArray:self.inventoryList Key:@"name"];
    self.resultArr = [YYPChineseToPinyin sortObjectArray:self.inventoryList Key:@"name"];

模拟加载库存数据:

- (void)loadInventoryData {
    NSArray *inventoryArr = [NSArray arrayWithObjects:
                              @"冰淇淋",@"土豆",@"##",
                              @"排骨",@"馒头",@"老婆饼",
                              nil];
    
    self.inventoryList = [[NSMutableArray alloc] initWithCapacity:0];
    for (int i = 0; i < [inventoryArr count]; i++) {
        YYPGoodsModel *good = [[YYPGoodsModel alloc] init];
        good.name = [inventoryArr objectAtIndex:i];
        good.num = i * 100;
        good.cost = i + 100;
        [self.inventoryList addObject:good];
    }
}

四. 添加搜索功能

// 保存搜索状态下的搜索数组
@property (strong, nonatomic) NSMutableArray *searchList;

添加手势去移除键盘:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self.searchBar resignFirstResponder];
}

- (void)tapAction{
    [self.tableView removeGestureRecognizer:_tap];
    [_searchBar resignFirstResponder];
}

使用代理UISearchBarDelegate做相应操作:

#pragma mark - UISearchBarDelegate -

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    if (!_tap) {
        _tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    }
    [self.tableView addGestureRecognizer:_tap];
    
    return YES;
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    
    [self.searchList removeAllObjects];
    
    // 模拟加载搜索数据
    [self loadSearchData];
    
    self.showSearch = YES;
    [self.tableView reloadData];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    
    if ([searchBar.text isEqualToString:@""]) {
        
        self.showSearch = NO;
        
        [self.tableView reloadData];
        
    } else {
        
        [self.searchList removeAllObjects];
        
        // 模拟加载搜索数据
        [self loadSearchData];
        
        self.showSearch = YES;
        [self.tableView reloadData];
    }
    
}

模拟加载搜索数据:

- (void)loadSearchData {
    NSArray *searchArr = [NSArray arrayWithObjects:
                              @"排骨",@"馒头",@"老婆饼",
                              nil];
    
    self.searchList = [[NSMutableArray alloc] initWithCapacity:0];
    for (int i = 0; i < [searchArr count]; i++) {
        YYPGoodsModel *search = [[YYPGoodsModel alloc] init];
        search.name = [searchArr objectAtIndex:i];
        search.num = i * 100;
        search.cost = i + 100;
        [self.searchList addObject:search];
    }
}

一般搜索来说是固定在顶部的,我这边是用的UIViewController, 将搜索块当作子视图放在内. 然后创建了myTableView去实现滚动这些页面.


五. 搜索界面复用库存界面, 增加标识判断显示界面

由于我们搜索界面就是在当前库存页面上搜索结果后直接展现,并不存在跳转下个界面,并且两个界面的 cell 布局完全一样. 这个时候只需要稍作处理复用即可(因为搜索不存在索引提示记得去掉索引title).

定义一个是否标识showSearch, 用于判断是否显示搜索结果

@property (assign, nonatomic) BOOL showSearch;

这个时候就修改上面的相关索引设置了, 增加判断若是搜索状态则不需要设置的逻辑:


#pragma mark - Table view data source

// 标签数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  
    if (self.showSearch) {
        return 1;
    }
    
    return self.indexArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (self.showSearch) {
        return self.searchList.count;
    }
    return [[self.resultArr objectAtIndex:section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YYPInventoryDetailsCell *cell =[YYPInventoryDetailsCell cellWithTableView:tableView];
    
    if (self.showSearch) {
        if (self.searchList.count) {
            YYPGoodsModel *model = self.searchList[indexPath.row];
            cell.model = model;
        }
        return cell;
    }
    
    YYPGoodsModel *model = [[self.resultArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    cell.model = model;
    return cell;
    
}
// 显示每组标题索引 (如果不实现 就不显示右侧索引)
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    
    
    if (self.showSearch) {
        return nil;
    }
    return self.indexArr;
}

// 设置索引section的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (self.showSearch) {
        return 0.0;
    }
    return 20;
}

// 返回每个索引的内容
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    if (self.showSearch) {
        return @"";
    }
    
    return self.indexArr[section]; 
}

// 响应点击索引时的委托方法(点击右侧索引表项时调用)
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    
    return index;
}

如果需要修改分区header 索引的文字颜色及背景颜色, 和在tableview 里设置表头一样的修改方法.

// 设置索引背景颜色及标题
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UI_View_Width, 20)];
    header.backgroundColor = YYPBackgroundColor;
   
    // Create label with section title
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(15, 0, UI_View_Width - 15, header.height);
    label.textColor = YYPColor(255, 255, 255);
    label.font = [UIFont systemFontOfSize:16.0];
    label.text = [[self.inventoryList objectAtIndex:section] valueForKey:@"name"];
    label.backgroundColor = [UIColor clearColor];
    [header addSubview:label];
    
    if (self.showSearch) {
        label.text = @"";
    } else {
        label.text = self.indexArr[section];
    }
    
    return header;
}

是不是思路很明了了,希望对大家有用!


索引排序搜索.gif

如果想看右侧那种无索引条, 类似索引布局接口页面请移步类似索引Model套Model之 iOS模型闲聊二

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,560评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,104评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,297评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,869评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,275评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,563评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,833评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,543评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,245评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,512评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,011评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,359评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,006评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,062评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,825评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,590评论 2 273
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,501评论 2 268

推荐阅读更多精彩内容