码迷,mamicode.com
首页 > 其他好文 > 详细

利用UILocalizedIndexedCollation写类似通讯录

时间:2016-04-05 19:23:46      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

//
//  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

技术分享

利用UILocalizedIndexedCollation写类似通讯录

标签:

原文地址:http://www.cnblogs.com/siasyl/p/5356108.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!