美文网首页
列表的索引功能SCIndexView及通讯录拼音分组排序BMCh

列表的索引功能SCIndexView及通讯录拼音分组排序BMCh

作者: 鄂北 | 来源:发表于2018-08-17 17:50 被阅读336次

在这里分享两个非常好用的列表的索引功能SCIndexView及通讯录拼音分组排序BMChineseStringSort


image.png

这里简单记录下两个的用法
SCIndexView:
先放上原作者的地址http://www.cocoachina.com/cms/wap.php?action=article&id=21851
用法:在拿到作者封装好的代码后,需导入一个头部文件

#import "UITableView+SCIndexView.h"

再创建tableview,代码如下

self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.backgroundColor = VIEW_BG_COLOR;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.barImageView.mas_bottom);
    }];
    
    if (@available(iOS 11.0, *)){
        // 这两个方法必须要,否则代理中设置组头/尾高度无限
        self.tableView.estimatedSectionHeaderHeight = 0;
        self.tableView.estimatedSectionFooterHeight = 0;
    }
    
    [self.view layoutIfNeeded];
    
    SCIndexViewConfiguration *configuration = [SCIndexViewConfiguration configurationWithIndexViewStyle:SCIndexViewStyleDefault];
    _tableView.sc_indexViewConfiguration = configuration;
    _tableView.sc_translucentForTableViewInNavigationBar = NO;

self.tableView.sc_indexViewDataSource = self.indexArray;

self.indexArray为tableview右边索引数据数组
self.indexArray数据是在BMChineseStringSort对数据进行处理后得到的,在下面有讲到

BMChineseStringSort
原作者的地址https://www.jianshu.com/p/b8de95b3175e
具体介绍请看原作者
用法:在拿到作者封装好的代码后,导入头文件

#import "BMChineseSort.h"

对自定义对象数组排序需要只需要使用两类个方法:

+(NSMutableArray*)IndexWithArray:(NSArray*)objectArray Key:(NSString *)key;
+(NSMutableArray*)sortObjectArray:(NSArray*)objectArray Key:(NSString *)key;

第一个方法:一个参数objectArray是自定义对象数组,另一个参数key是数组里需要排序的字段名字。方法返回所有出现过的首字母,用于显示在tableview的head以及右侧索引缩写。
第二个方法:,是根据对象的某个字段值对整个数组进行排序,首先,先将字段首字母拼音相同的对象存到同一个数组里,然后把所有的数组再放到结果数组里

实际应用:

//根据Person对象的 name 属性 按中文 对 Person数组 排序
                self.indexArray = [BMChineseSort IndexWithArray:resultArr Key:@"name"];
                self.tableArr = [BMChineseSort sortObjectArray:resultArr Key:@"name"];

self.indexArray为自定义接收数组

NOTE:最近作者有更新整体速度提升了一倍,有兴趣的同学可以直接传送到原作者

相关文章

网友评论

      本文标题:列表的索引功能SCIndexView及通讯录拼音分组排序BMCh

      本文链接:https://www.haomeiwen.com/subject/yfetiftx.html