//
// AddTeamMeberViewController.h
// HengTaiXinGolf
//
// Created by 欧阳荣 on 16/4/21.
// Copyright © 2016年 HengTaiXinGolf. All rights reserved.
//
#import "ParentsViewController.h"
@interface AddTeamMeberViewController : ParentsViewController
{
NSMutableArray *_dataSouceArray;//所有的汉子数组
NSMutableDictionary *nameDic;//key:姓 value:对应姓的NSArray
}
@property (nonatomic,strong) NSString * teamNo;
@property (nonatomic,strong) NSString * teamName;
@property(nonatomic,strong) UITableView *m_tableView;
@property(nonatomic,strong) NSMutableArray *m_allName;
@property (nonatomic,strong) UIButton *btn;
@property (nonatomic,strong) UIButton *btn1;
@end
//
// AddTeamMeberViewController.m
// HengTaiXinGolf
//
// Created by 欧阳荣 on 16/4/21.
// Copyright © 2016年 HengTaiXinGolf. All rights reserved.
//
#import "AddTeamMeberViewController.h"
#import "MemberBeanModel.h"
#import "AddTMCell.h"
#import "KCPinyinHelper.h"
#define ADDTECELL @"AddTMCell"
@interface AddTeamMeberViewController ()<UITableViewDataSource,UITableViewDelegate>
@end
@implementation AddTeamMeberViewController{
NSMutableArray *_nameDataArr;
NSMutableArray * _selecteArr;
}
@synthesize m_tableView;
@synthesize m_allName;
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil];
if (self)
{
_nameDataArr = [NSMutableArray array];
_dataSouceArray = [NSMutableArray array];
_selecteArr = [NSMutableArray array];
UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0 , 100, 44)];
titleLabel.backgroundColor = [UIColor clearColor]; //设置Label背景透明
titleLabel.font = [UIFont boldSystemFontOfSize:KTitle]; //设置文本字体与大小
titleLabel.textColor = [UIColor whiteColor]; //设置文本颜色
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.text = @"选择联系人"; //设置标题
self.navigationItem.titleView = titleLabel;
}
return self;
}
//增加右键发表保存按钮
-(void)createRightBtn{
_btn = [UIButton buttonWithType:UIButtonTypeCustom];
_btn.frame = CGRectMake(0, 0, 40, 20);
[_btn setTitle:@"确定" forState:UIControlStateNormal];
[_btn addTarget:self action:@selector(rightBtnClicked) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:_btn];
self.navigationItem.rightBarButtonItem = item;
}
-(void)createLeftBtn{
_btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
_btn1.frame = CGRectMake(0, 0, 40, 20);
[_btn1 setTitle:@"取消" forState:UIControlStateNormal];
[_btn1 addTarget:self action:@selector(leftBtnClicked) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * item1 = [[UIBarButtonItem alloc]initWithCustomView:_btn1];
self.navigationItem.leftBarButtonItem = item1;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
[self createLeftBtn];
[self createRightBtn];
[self createData];
[self geturl:KgetAddTeamMemberList];
}
-(void)rightBtnClicked{
DSLog(@"确定");
[self setTeamMemberAddUrl];
}
-(void)leftBtnClicked{
DSLog(@"取消");
[self.navigationController popViewControllerAnimated:YES];
}
-(void)setTeamMemberAddUrl
{
AFHTTPRequestOperationManager *man = [AFHTTPRequestOperationManager manager];
man.responseSerializer = [AFHTTPResponseSerializer serializer];
man.requestSerializer = [AFJSONRequestSerializer serializer];//用json格式来上传参数
NSString *stringarr=[NSString string];
NSString *string;//[NSMutableString string];
for (int i=0; i<_selecteArr.count; i++) {
string=[_selecteArr objectAtIndex:i];
stringarr=[stringarr stringByAppendingString:[NSString stringWithFormat:@"%@,",string]];
}
NSString *string2=[stringarr substringToIndex:stringarr.length-1];
NSString *mynoarray =[NSString stringWithFormat:@"(%@)",string2];
DSLog(@"%@",mynoarray);
NSString * memNick = [UserModel sharedInstance].memberNick;
NSString *paraString = [MD5 encodeString:memNick];
NSString * teaM = [MD5 encodeString:self.teamName];
NSString *memStr = [UserModel sharedInstance].memberId;
//拒绝好友请求参数
NSMutableDictionary * _param = [NSMutableDictionary dictionary];
[_param setValue:memStr forKey:@"memberId"];
[_param setValue:paraString forKey:@"memberNick"];
[_param setValue:self.teamNo forKey:@"teamNo"];
[_param setValue:teaM forKey:@"teamName"];
[_param setValue:mynoarray forKey:@"memberIdArray"];
[_param setValue:KAPPID forKey:@"appId"];
[_param setValue:KappSecret forKey:@"appSecret"];
[_param setValue:[CommonTools getTimeScamp] forKey:@"timestamp"];
NSArray * paramArr = @[[NSString stringWithFormat:@"memberId%@",_param[@"memberId"]],[NSString stringWithFormat:@"memberNick%@",_param[@"memberNick"]],[NSString stringWithFormat:@"teamNo%@",_param[@"teamNo"]],[NSString stringWithFormat:@"teamName%@",_param[@"teamName"]],[NSString stringWithFormat:@"memberIdArray%@",_param[@"memberIdArray"]],[NSString stringWithFormat:@"appId%@",_param[@"appId"]],[NSString stringWithFormat:@"appSecret%@",_param[@"appSecret"]],[NSString stringWithFormat:@"timestamp%@",_param[@"timestamp"]]];
[_param setValue:[CommonTools tokenmd5StringFromArray:paramArr] forKey:@"digest"];
[man POST:KsetTeamMemberAdd parameters:_param success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
NSLog(@" 邀请好友 :dict %@",dict);
if ([dict[@"returnCode"] isEqualToString:@"000000"]) {
[MBProgressHUD showSuccess:@"邀请成功" toView:nil];
[self.navigationController popViewControllerAnimated:YES];
} else
{
[MBProgressHUD showSuccess:@"邀请失败" toView:nil];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error %@",error);
}];
}
-(void)geturl:(NSString *)url
{
NSString *memStr = [UserModel sharedInstance].memberId;
//判断是否好友请求参数 getAddTeamMemberList
NSMutableDictionary * _param = [NSMutableDictionary dictionary];
[_param setValue:memStr forKey:@"memberId"];
[_param setValue:KAPPID forKey:@"appId"];
[_param setValue:KappSecret forKey:@"appSecret"];
[_param setValue:[CommonTools getTimeScamp] forKey:@"timestamp"];
NSArray * paramArr = @[[NSString stringWithFormat:@"memberId%@",_param[@"memberId"]],[NSString stringWithFormat:@"appId%@",_param[@"appId"]],[NSString stringWithFormat:@"appSecret%@",_param[@"appSecret"]],[NSString stringWithFormat:@"timestamp%@",_param[@"timestamp"]]];
[_param setValue:[CommonTools tokenmd5StringFromArray:paramArr] forKey:@"digest"];
[HTTPRequestTool sendGetAFRequest:url withParameters:_param withSuccess:^(id message) {
NSDictionary *tempArr = [NSJSONSerialization JSONObjectWithData:message options:NSJSONReadingMutableContainers error:nil];
/**
* 请求数据 (判断数据请求的类型)
*/
DSLog(@" ----获取添加球队成员列表接口-- %@",tempArr);
if (_nameDataArr||_dataSouceArray) {
[_nameDataArr removeAllObjects];
[_dataSouceArray removeAllObjects];
}
for (NSDictionary *tempDic in tempArr[@"teamMemberList"])
{
MemberBeanModel *contactModel = [[MemberBeanModel alloc]init];
[contactModel setValuesForKeysWithDictionary:tempDic];
[_dataSouceArray addObject:contactModel];//所有好友信息数组
[_nameDataArr addObject:contactModel.memberNick];//所有好友昵称数组
}
[self reloadDataUI];
} andWithFail:^(id message) {
DSLog(@"error = %@",message);
}];
}
-(void)reloadDataUI{
DSLog(@"====== 所有人的名字 _nameDataArr======%@",_nameDataArr);
self.m_allName = [[NSMutableArray alloc]init];
for (int i = 0; i < _nameDataArr.count; i ++) {
char firstChar = pinyinFirstLetter([[_nameDataArr objectAtIndex:i]characterAtIndex:0]);
NSString *youName = [NSString stringWithFormat:@"%c",firstChar];
//不添加重复元素
if (![m_allName containsObject:[youName uppercaseString]]) {
[self.m_allName addObject:[youName uppercaseString]];
}
}
DSLog(@"======= 所有人名字的首字母大写 =m_allName========%@",self.m_allName);//所有的索引大写字母
[self.m_allName sortUsingSelector:@selector(compare:)];//给所有索引大写字母排序
nameDic = [[NSMutableDictionary alloc]init];//每个大写字母对应的名字字典
//每个section对应的行数列表
for (NSString *sectionString in m_allName) {
NSMutableArray *rowSource = [[NSMutableArray alloc]init];
for (NSString *charString in _nameDataArr) {
char firstChar = pinyinFirstLetter([charString characterAtIndex:0]);
NSString *youName = [NSString stringWithFormat:@"%c",firstChar];
if ([sectionString isEqualToString:[youName uppercaseString]]) {
[rowSource addObject:charString];
}
}
[nameDic setValue:rowSource forKey:sectionString];
}
DSLog(@"======nameDic======%@",nameDic);//每个大写字母对应的名字字典
[self.m_tableView reloadData];
}
-(void)createData{
self.m_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, IPHONE_HEIGHT-64)style:UITableViewStylePlain];
[self.m_tableView registerClass:[AddTMCell class] forCellReuseIdentifier:ADDTECELL];
self.m_tableView.delegate = self;
self.m_tableView.dataSource = self;
self.m_tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
[self.view addSubview:self.m_tableView];
}
#pragma mark - UITableView delegate
//m_allName 大写字母 nameDic 所有字母对应的名字
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSArray *dicCount = [nameDic objectForKey:[m_allName objectAtIndex:section]];
return [dicCount count];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [m_allName count];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [m_allName objectAtIndex:section];
}
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
//添加通讯录索引
NSMutableArray *toBeRetured = [[NSMutableArray alloc]init];
for (char c = 'A'; c <= 'Z'; c ++) {
[toBeRetured addObject:[NSString stringWithFormat:@"%c",c]];
}
return toBeRetured;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 36 * KSCALE_X;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
AddTMCell *cell = [tableView dequeueReusableCellWithIdentifier:ADDTECELL];
if (cell == nil) {
cell = [[AddTMCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ADDTECELL];
}
// cell.selectionStyle = UITableViewCellSelectionStyleNone;
//每个字母对应的名字
NSString *sectionString = [m_allName objectAtIndex:indexPath.section];
NSArray *allShowName = [nameDic objectForKey:sectionString];
if (allShowName.count > 0) {
cell.nameLabel.text = [allShowName objectAtIndex:indexPath.row];
}
//_dataSouceArray 所有人的数据模型 --- 在通过昵称去找用户的头像
for (MemberBeanModel *contactModel in _dataSouceArray){
if ([contactModel.memberNick isEqualToString:[allShowName objectAtIndex:indexPath.row]]) {
CGSize nameSize = [contactModel.memberNick sizeWithAttributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:15 * KWIDTH] forKey:NSFontAttributeName]];
if (nameSize.width < 200 * KSCALE_X ) {
cell.nameLabel.frame =CGRectMake(155 * KSCALE_X, 31 * KSCALE_X, nameSize.width, 31 * KSCALE_X);
}
if ([contactModel.memberTeam length]) {
cell.selectImgView.image = [UIImage imageNamed:@"hyyj"];
cell.teamNameLab.text = [NSString stringWithFormat:@"(%@)",contactModel.memberTeam];
cell.selectImgView.tag = 0;
}else{
cell.selectImgView.tag = 1;
cell.selectImgView.image = [UIImage imageNamed:@"hywx"];
cell.teamNameLab.text = @"";
}
__block UIActivityIndicatorView *activityIndicator;
NSString *urlStr = [NSString stringWithFormat:@"%@thumb/%@",KimgUrl,contactModel.memberNickImg];
[cell.iconImgView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"mctx_wode"] options:SDWebImageProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize) {
if (!activityIndicator)
{
activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.center = cell.iconImgView.center;
dispatch_async(dispatch_get_main_queue(), ^{
[cell.iconImgView addSubview:activityIndicator];
});
[activityIndicator startAnimating];
}
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
[activityIndicator stopAnimating];
}];
}
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
AddTMCell *cell = [self.m_tableView
cellForRowAtIndexPath:indexPath ];
DSLog(@" teamNameLab %@",cell.teamNameLab.text);
DSLog(@" selectImgView.tag %ld ",(long)cell.selectImgView.tag);
MemberBeanModel *contactListModel;
NSString *sectionString = [m_allName objectAtIndex:indexPath.section];
NSArray *allShowName = [nameDic objectForKey:sectionString];
if (allShowName.count > 0) {
contactListModel.memberNick = [allShowName objectAtIndex:indexPath.row];
}
//_dataSouceArray 所有人的数据模型 再次通过名字来找用户的ID
for (MemberBeanModel *contactModel in _dataSouceArray){
if ([contactModel.memberNick isEqualToString:[allShowName objectAtIndex:indexPath.row]]) {
DSLog(@" hah 我选了 %@",contactModel.memberNick);
if (cell.selectImgView.tag == 0) {
DSLog(@"已加入球队了的");
return;
}else if (cell.selectImgView.tag == 1){
DSLog(@"没选 。。。。 选上吧 ");
cell.selectImgView.image = [UIImage imageNamed:@"hyyx"];
cell.selectImgView.tag = 2;
[_selecteArr addObject:contactModel.memberId];
}else if (cell.selectImgView.tag == 2){
DSLog(@"选了的 现在不选了");
cell.selectImgView.tag = 1;
cell.selectImgView.image = [UIImage imageNamed:@"hywx"];
[_selecteArr removeObject:contactModel.memberId];
}
}
}
}
// 根据索引来找姓名字母
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
NSInteger count = 0;
//m_allName 所有的姓的大写字母 跟索引的字母比较
for (NSString *character in m_allName) {
if ([character isEqualToString:title]) {
return count;
}
count ++;
}
return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 93 * KSCALE_X;
}
- (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