利用UILocalizedIndexedCollation写类似通讯录

//
//  MyConcernListViewController.m
//  loopdiner
//
//  Created by yl on 16/4/5.
//  Copyright © 2016年 yl. All rights reserved.
//

#import "MyConcernListViewController.h"
#import "PeopleCell.h"
#import "FansAndCnnModel.h"

@interface MyConcernListViewController () {
    NSMutableArray *_dataSourceArray;//数据源数数组
    NSMutableArray *_userInfosArray;//所有用户的信息
    NSMutableArray *_indexArray;//右侧索引
}

@property (nonatomic,copy) UITableView *tableView;

@end

@implementation MyConcernListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view

    _dataSourceArray = [[NSMutableArray alloc] init];
    _userInfosArray = [[NSMutableArray alloc] init];
    _indexArray = [[NSMutableArray alloc] init];
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    [_tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];

    //获取所有关注的人
    [self getAllMyCnn];
}

#pragma mark 获取所有关注的人
- (void)getAllMyCnn {
    NSMutableDictionary *paraDict = [LDUnity getParaDict];
    NSDictionary *dataDict = @{
                               @"fans_user_id":MyUserId
                               };
    [paraDict setObject:dataDict forKey:@"data"];
    [self.loadingView startAnimating];
    [[LPAFRequest sharedRequest] postRequestWithTableName:Table_UserFansCnn operation:Operation_GetCnn parameter:paraDict successBlock:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"查询所有我关注的人%@",responseObject);
        if ([responseObject[@"success"] boolValue]) {
            for (NSDictionary *dict in responseObject[@"data"]) {
                if (dict[@"user_cnn"]) {
                    UserModel *model = [[UserModel alloc] initWithDictionary:dict[@"user_cnn"] error:nil];
                    [_userInfosArray addObject:model];

                }
            }
            [self divideIntoGroupsWithSourceArray:_userInfosArray];
        } else {
            self.noContentLabel.text = @"暂时没有关注的人";
            self.noContentLabel.hidden = NO;
            [self.view bringSubviewToFront:self.noContentLabel];
        }
    } failedBlock:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.loadingView stopAnimating];
    }];

}

- (void)divideIntoGroupsWithSourceArray:(NSMutableArray *)array {
    //初始化
    UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
    //得到collation索引的数量 27个 = a~z 和 #
    NSInteger indexCount = [[collation sectionIndexTitles] count];
    //根据索引数量初始化一个存放所有索引内容的数组
    NSMutableArray *dataArray = [[NSMutableArray alloc] initWithCapacity:indexCount];
    for (int i = 0; i < indexCount; i++) {
        NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
        [dataArray addObject:mutableArray];
    }
    for (UserModel *model in array) {
        //获取name属性的值所在的位置,比如"林丹",首字母是L,在A~Z中排第11(第一位是0),sectionNumber就为11
        NSInteger index = [collation sectionForObject:model collationStringSelector:@selector(name)];
        //把name添加到对应的数组中
        NSMutableArray *mutableArray = dataArray[index];
        [mutableArray addObject:model];
    }

    //对每个section中的数组按照name属性排序
    for (int i = 0; i < indexCount; i++) {
        //遍历数组中的元素,每个元素也是一个数组
        NSMutableArray *personArray = dataArray[i];
        //将数组中元素依据昵称进行排序
        NSArray *newPersonArray = [collation sortedArrayFromArray:personArray collationStringSelector:@selector(name)];
        dataArray[i] = newPersonArray;
        if (personArray.count) {
            //如果数组不为空,把title添加到索引数组中
            [_indexArray addObject:[collation sectionIndexTitles][i]];
        }
    }

    _dataSourceArray = [dataArray mutableCopy];
    [_tableView reloadData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PeopleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[PeopleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    UserModel *model = _dataSourceArray[indexPath.section][indexPath.row];
    [cell.headImg sd_setImageWithURL:[NSURL URLWithString:model.portrait] placeholderImage:[UIImage imageNamed:@"pic"]];
    cell.nameLabel.text = model.name;
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_dataSourceArray[section] count];
}

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

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if ([_dataSourceArray[section] count]) {
        return 20;
    } else {
        return 0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 55;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if ([_dataSourceArray[section] count]) {
        return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles][section];
    } else {
        return @"";
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

//开启右侧索引条
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return _indexArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

时间: 2024-08-08 01:13:33

利用UILocalizedIndexedCollation写类似通讯录的相关文章

Android实现固定头部信息,挤压动画(类似通讯录)

半年前,那时候我还是个大四的学生,每天都在找工作度过,想去北京体验一下蚁族生活,奋然离开了济南,哎...在济南我们学校还是数得着的好学校,去了北京就什么都不是了,一切的辛酸只有自己知道,那时候的我只找开发方面的工作,也第一次体会到了女生的劣势... 在找工作的过程中,大部分都是实习,跟我的预想有点冲突,去了XX集团,人家只要211,985的,没办法,去了XX工厂收到了类似通讯录的这么个任务... 废话少说,开始今天的主题 在很多应用中,看到这样的listview:listview滑动过程中分组标

[前端引用] 利用ajax实现类似php include require 等命令的功能

利用ajax实现类似php include require 等命令的功能 需求介绍 在写页面的时候,一个导航可能N页面要用到,每一个页面都需要复制粘贴一份进去,一旦有改动html代码的需求就苦逼了,每个都得改一遍.如果这个需求还很多,反复改,那简直就是折磨人啊(虽然这种情况一般的情况下遇不到,但是偶老是遇到.) 直接看代码 <script id="ajax-header" type="text/javascript"> //通过js创建外层div $(&

[DevExpress]利用LookUpEdit实现类似自动提示效果

原文:[DevExpress]利用LookUpEdit实现类似自动提示效果 关键代码: public static void BindWithAutoCompletion(this LookUpEdit lue, object source, string value, string displayName, string prompttext) { lue.Properties.DataSource = source; lue.Properties.DisplayMember = displa

利用TraceSource写日志

利用TraceSource写日志 从微软推出第一个版本的.NET Framework的时候,就在“System.Diagnostics”命名空间中提供了Debug和Trace两个类帮助我们完成针对调试和跟踪信息的日志记录.在.NET Framework 2.0中,微软引入了TraceSource并对跟踪日志系统进行了优化,优化后的跟踪日志系统在.NET Core中又经过了相应的简化..NET Core的日志模型借助TraceSourceLoggerProvider实现对TraceSource的整

利用FlashPaper实现类似百度文库功能

最近需要实现一个类似百度文库的功能,在Google上淘了一段时间,发现FlashPaper还算能够不错的实现此需求. 首先讲下思路: 1>安装FlashPaper: 2>利用java代码将doc等文件转换为swf文件: 3>页面展示swf即可. 关于FlashPaper的使用及安装这里就不再赘述,网上的资料很多,可以自行Google. 下面是java下如何利用FlashPaper将doc等文件转化为swf文件--> @param swfName 指定生成的swf文件名@param

利用html5实现类似微信的手机摇一摇功能

利用html5实现类似微信的手机摇一摇功能,并播放音乐. 1.  deviceOrientation:封装了方向传感器数据的事件,可以获取手机静止状态下的方向数据,例如手机所处角度.方位.朝向等. 2.  deviceMotion:封装了运动传感器数据的事件,可以获取手机运动状态下的运动加速度等数据. 不多说直接上代码, Javascript: [javascript] view plaincopy var SHAKE_THRESHOLD = 3000; var last_update = 0;

iOS 实现类似通讯录按拼音排序 - PinYin4Objc

项目中需要实现类似通讯录那样按拼音进行排序以及索引列表的显示的功能,我这里使用了 PinYin4Objc 这个库来实现此功能. PinYinObjc是一个效率很高的汉字转拼音类库,智齿简体和繁体中文,有如下特点: 1.效率高,使用数据缓存,第一次初始化以后,拼音数据存入文件缓存和内存缓存,后面转换效率大大提高: 2.支持自定义格式化,拼音大小写等等: 3.拼音数据完整,支持中文简体和繁体,与网络上流行的相关项目比,数据很全,几乎没有出现转换错误的问题. 下载 PinYinObjc 项目中的实际应

利用python写出1加到任意数关于range()函数妙用的交互小程序

认真!????? ------------ 利用Python写的一个从1加到任意大小的数字(电脑吃得消再说)的交互脚本. 在这里我运用了range函数(我喜欢叫它蟒岭函数,因为range有“岭”的意思),此函数可以有个有趣的用法:range(x),解释器会列出从0~x-1的所有整数:如果是range(y,x),解释器就会列出从从y~x-1的所有整数(x-1 等价于 不包括x,应该能够理解吧):还有一种range(y,x,z),解释器就会跳跃z列出从y~x-1的所有整数,默认的跳跃间距为1,就如r

利用yield写斐波拉契(python)

利用yield写斐波拉契(python) by 伍雪颖 def fib(n): a = b = 1 for i in range(n): yield a a,b = b, a+ b print list(fib(10))