//
// 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
![](https://images2015.cnblogs.com/blog/856756/201604/856756-20160405175445047-1185159565.png)