@interface SJZFriendListController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>
/**
* 好友列表的tableView
*/
@property (nonatomic,strong) UITableView *tableView;
/**
* 上面的搜索条
*/
@property (nonatomic,strong) UISearchBar *searchBar;
/**
* 所有好友数组
*/
@property (nonatomic,strong) NSArray *allFirendDataArray;
@property (nonatomic,strong) NSMutableArray *allNewFirendDataArray;
/**
* 所有名字字典
*/
@property (nonatomic,strong) NSMutableDictionary *allNameDictionary;
/**
* 搜索结果的数组
*/
@property (nonatomic,strong) NSMutableArray *searchArr;//搜索到的内容
/**
* 用户好友数组
*/
@property(nonatomic,strong) NSMutableArray *useridArr;
/**
* 记录多选选中的行
*/
@property (strong, nonatomic) NSMutableArray *selectIndexs;//多选选中的行
@end
@implementation SJZFriendListController
- (void)viewDidLoad {
[super viewDidLoad];
_selectIndexs = [[NSMutableArray alloc]init];
[self setBaseVCAttributesWithTitle:nil left:@"fanhui.png" right:@"完成"];
// self.useridArr =[[NSMutableArray alloc]init];
_allNewFirendDataArray = [[NSMutableArray alloc] init];
_searchArr = [[NSMutableArray alloc] init];
_allNameDictionary = [[NSMutableDictionary alloc] init];
[self initSelfView];
[self getDataWithState:refreshForFirst];
}
#pragma mark - UISet 设置视图UI
- (void)initSelfView{
//搜索按钮
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 50)];
_searchBar.delegate = self;
_searchBar.placeholder = @"请输入好友中文名或者拼音";
_searchBar.searchBarStyle = UISearchBarStyleDefault;
_searchBar.barTintColor = RGBA(245, 245, 245, 1);
//在键盘上部添加一个隐藏按钮
UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
inputView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1.0];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(inputView.frame.size.width-50, 0, 50, inputView.frame.size.height);
[btn setTitle:@"完成" forState:UIControlStateNormal];
btn.titleLabel.font = [UIFont systemFontOfSize:16];
[btn addTarget:self action:@selector(onHideKeyboard) forControlEvents:UIControlEventTouchUpInside];
[inputView addSubview:btn];
_searchBar.inputAccessoryView = inputView;
[self.view addSubview:_searchBar];
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64 + 49, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.sectionHeaderHeight = 20;
_tableView.rowHeight = 50;
_tableView.tableHeaderView =[[UIView alloc]init];
_tableView.tableFooterView = [[UIView alloc]init];
// _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.sectionFooterHeight = 0.1;
[_tableView registerClass:[MyFriendSelectTableViewCell class] forCellReuseIdentifier:@"selectFriend"];
[self.view addSubview:_tableView];
}
-(NSMutableArray *)useridArr{
if (nil==_useridArr) {
_useridArr =[[NSMutableArray alloc]init];
NSArray *arr = [NIMSDK sharedSDK].userManager.myFriends;
for (NIMUser *user in arr) {
[self.useridArr addObject:user.userId];
}
}
return _useridArr;
}
#pragma mark - Http 网络请求方法
- (void)getDataWithState:(RefreshState)state{
for (NSString *str in self.useridArr) {
if (str.length > 0) {
MyFriendModel *model = [[MyFriendModel alloc] init];
model.name = str;
//获取首字,uppercaseString是将首字母转换成大写
model.namePinyin = [NSString chineseToPinyin:str];
NSString *letterStr = [[model.namePinyin substringWithRange:NSMakeRange(0, 1)] uppercaseString];
model.nameLetter = letterStr;
[_allNewFirendDataArray addObject:model];
model.ifSelected =NO;
}
}
//将首字母相同的放在一起
[self getAllNames];
}
#pragma mark - 将首字母相同的放在一起
- (void)getAllNames
{
//遍历
for (MyFriendModel *model in _allNewFirendDataArray) {
NSMutableArray *letterArr = _allNameDictionary[model.nameLetter];
//判断数组里是否有元素,如果为nil,则实例化该数组,并在cityDict字典中插入一条新的数据
if (letterArr == nil) {
letterArr = [[NSMutableArray alloc] init];
[_allNameDictionary setObject:letterArr forKey:model.nameLetter];
}
//将新数据放到数组里
[letterArr addObject:model];
}
}
#pragma mark - 获得所有的key值并排序,并返回排好序的数组
- (NSArray *)getCityDictAllKeys
{
//获得cityDict字典里的所有key值,
NSArray *keys = [_allNameDictionary allKeys];
//打印
//按升序进行排序(A B C D……)
return [keys sortedArrayUsingSelector:@selector(compare:)];
}
#pragma mark - TableView tableview相关delete和datasource
//引入索引的一个代理方法
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
NSArray *keys = [self getCityDictAllKeys];//获得所有的key值
return keys;
}
//section上的标题(A B C D……Z)
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSArray *keys = [self getCityDictAllKeys];//获得所有的key值(A B C D……Z)
return keys[section];
}
//section的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
NSArray *keys = [self getCityDictAllKeys];//获得所有的key值
return keys.count;
}
//每个section对应的cell的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSArray *keys = [self getCityDictAllKeys];//获得所有的key值
NSString *keyStr = keys[section];//(A B C D……Z)
NSArray *array = [_allNameDictionary objectForKey:keyStr];//所有section下key值所对应的value的值
return array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyFriendSelectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"selectFriend" forIndexPath:indexPath];
NSArray *keys = [self getCityDictAllKeys];//获得所有的key值
NSString *keyStr = keys[indexPath.section];
NSArray *array = [_allNameDictionary objectForKey:keyStr];//所有section下key值所对应的value的值,array就是value值,存放的是model模型
MyFriendModel *model = [array objectAtIndex:indexPath.row];
cell.headImageView.image = [UIImage imageNamed:@"headerImage.jpg"];
cell.nameLabel.text = model.name;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//设置勾
// cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectButton.selected = NO;
for (NSIndexPath *index in _selectIndexs) {
if (index == indexPath) { //改行在选择的数组里面有记录
cell.selectButton.selected =YES; //打勾
break;
}
}
return cell;
}
//点击每个cell触发的事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//cell的多选操作
//获取到点击的cell
MyFriendSelectTableViewCell *cell = (MyFriendSelectTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
if (cell.selectButton.selected==YES) { //如果为选中状态
cell.selectButton.selected=NO; //切换为未选中
[_selectIndexs removeObject:indexPath]; //数据移除
}else { //未选中
cell.selectButton.selected=YES; //切换为选中
[_selectIndexs addObject:indexPath]; //添加索引数据到数组
}
// MyFriendSelectTableViewCell *cell = (MyFriendSelectTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
// cell.accessoryType = UITableViewCellAccessoryCheckmark;
// FriendMessageViewController *message = [[FriendMessageViewController alloc]init];
// NSArray *keys = [self getCityDictAllKeys];//获得所有的key值
// NSString *keyStr = keys[indexPath.section];
// NSArray *array = [_allNameDictionary objectForKey:keyStr];//所有section下key值所对应的value的值,array就是value值,存放的是model模型
// MyFriendModel *model = [array objectAtIndex:indexPath.row];
// message.userId = model.name;
// [self.navigationController pushViewController:message animated:YES];
// FriendInfoViewController *vc = [[FriendInfoViewController alloc] init];
// [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - UISearchBar - delegate
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
BOOL isSearch = YES;//有编辑内容时为YES
if (searchText.length <= 0) {
isSearch = NO;//被清空时为NO
}
NSString *searchStr = _searchBar.text;
[_searchArr removeAllObjects];//清空searchDataArr,防止显示之前搜索的结果内容
//把这个文本与数据源进行比较
//把数据源中类似的数据取出,存入searchDataArr
for (NSInteger i= 0;i < _allNewFirendDataArray.count; i ++){
MyFriendModel *model = _allNewFirendDataArray[i];
searchStr = [searchStr lowercaseString];//转换成小写
BOOL isHas = [model.name hasPrefix:searchStr];//判断model.name是否以字符串searchStr开头
if(isHas){
[_searchArr addObject:model];
}else{
isHas = [model.namePinyin hasPrefix:searchStr];
if (isHas) {
[_searchArr addObject:model];
}
}
}
if (searchStr.length>0) {
[_allNameDictionary removeAllObjects];
//遍历
for (MyFriendModel *model in _searchArr) {
NSMutableArray *letterArr = _allNameDictionary[model.nameLetter];
//判断数组里是否有元素,如果为nil,则实例化该数组,
if (letterArr == nil) {
letterArr = [[NSMutableArray alloc] init];
[_allNameDictionary setObject:letterArr forKey:model.nameLetter];
}
[letterArr addObject:model];
}
}else{
//遍历
for (MyFriendModel *model in _allNewFirendDataArray) {
NSMutableArray *letterArr = _allNameDictionary[model.nameLetter];
//判断数组里是否有元素,如果为nil,则实例化该数组,
if (letterArr == nil) {
letterArr = [[NSMutableArray alloc] init];
[_allNameDictionary setObject:letterArr forKey:model.nameLetter];
}
[letterArr addObject:model];
}
}
[_tableView reloadData];
}
#pragma mark - ButtonAction 按钮事件
- (void)onHideKeyboard{
[_searchBar resignFirstResponder];
}
- (void)leftSelectedEvent:(id)sender{
[self.navigationController popViewControllerAnimated:YES];
}
- (void)rightSelectedEvent:(id)sender{
// [[NIMSDK sharedSDK].userManager deleteFriend:@"111111" completion:nil]; //删除好友
}
/**
* 好友列表的tableView
*/
@property (nonatomic,strong) UITableView *tableView;
/**
* 上面的搜索条
*/
@property (nonatomic,strong) UISearchBar *searchBar;
/**
* 所有好友数组
*/
@property (nonatomic,strong) NSArray *allFirendDataArray;
@property (nonatomic,strong) NSMutableArray *allNewFirendDataArray;
/**
* 所有名字字典
*/
@property (nonatomic,strong) NSMutableDictionary *allNameDictionary;
/**
* 搜索结果的数组
*/
@property (nonatomic,strong) NSMutableArray *searchArr;//搜索到的内容
/**
* 用户好友数组
*/
@property(nonatomic,strong) NSMutableArray *useridArr;
/**
* 记录多选选中的行
*/
@property (strong, nonatomic) NSMutableArray *selectIndexs;//多选选中的行
@end
@implementation SJZFriendListController
- (void)viewDidLoad {
[super viewDidLoad];
_selectIndexs = [[NSMutableArray alloc]init];
[self setBaseVCAttributesWithTitle:nil left:@"fanhui.png" right:@"完成"];
// self.useridArr =[[NSMutableArray alloc]init];
_allNewFirendDataArray = [[NSMutableArray alloc] init];
_searchArr = [[NSMutableArray alloc] init];
_allNameDictionary = [[NSMutableDictionary alloc] init];
[self initSelfView];
[self getDataWithState:refreshForFirst];
}
#pragma mark - UISet 设置视图UI
- (void)initSelfView{
//搜索按钮
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 50)];
_searchBar.delegate = self;
_searchBar.placeholder = @"请输入好友中文名或者拼音";
_searchBar.searchBarStyle = UISearchBarStyleDefault;
_searchBar.barTintColor = RGBA(245, 245, 245, 1);
//在键盘上部添加一个隐藏按钮
UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
inputView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1.0];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(inputView.frame.size.width-50, 0, 50, inputView.frame.size.height);
[btn setTitle:@"完成" forState:UIControlStateNormal];
btn.titleLabel.font = [UIFont systemFontOfSize:16];
[btn addTarget:self action:@selector(onHideKeyboard) forControlEvents:UIControlEventTouchUpInside];
[inputView addSubview:btn];
_searchBar.inputAccessoryView = inputView;
[self.view addSubview:_searchBar];
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64 + 49, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.sectionHeaderHeight = 20;
_tableView.rowHeight = 50;
_tableView.tableHeaderView =[[UIView alloc]init];
_tableView.tableFooterView = [[UIView alloc]init];
// _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.sectionFooterHeight = 0.1;
[_tableView registerClass:[MyFriendSelectTableViewCell class] forCellReuseIdentifier:@"selectFriend"];
[self.view addSubview:_tableView];
}
-(NSMutableArray *)useridArr{
if (nil==_useridArr) {
_useridArr =[[NSMutableArray alloc]init];
NSArray *arr = [NIMSDK sharedSDK].userManager.myFriends;
for (NIMUser *user in arr) {
[self.useridArr addObject:user.userId];
}
}
return _useridArr;
}
#pragma mark - Http 网络请求方法
- (void)getDataWithState:(RefreshState)state{
for (NSString *str in self.useridArr) {
if (str.length > 0) {
MyFriendModel *model = [[MyFriendModel alloc] init];
model.name = str;
//获取首字,uppercaseString是将首字母转换成大写
model.namePinyin = [NSString chineseToPinyin:str];
NSString *letterStr = [[model.namePinyin substringWithRange:NSMakeRange(0, 1)] uppercaseString];
model.nameLetter = letterStr;
[_allNewFirendDataArray addObject:model];
model.ifSelected =NO;
}
}
//将首字母相同的放在一起
[self getAllNames];
}
#pragma mark - 将首字母相同的放在一起
- (void)getAllNames
{
//遍历
for (MyFriendModel *model in _allNewFirendDataArray) {
NSMutableArray *letterArr = _allNameDictionary[model.nameLetter];
//判断数组里是否有元素,如果为nil,则实例化该数组,并在cityDict字典中插入一条新的数据
if (letterArr == nil) {
letterArr = [[NSMutableArray alloc] init];
[_allNameDictionary setObject:letterArr forKey:model.nameLetter];
}
//将新数据放到数组里
[letterArr addObject:model];
}
}
#pragma mark - 获得所有的key值并排序,并返回排好序的数组
- (NSArray *)getCityDictAllKeys
{
//获得cityDict字典里的所有key值,
NSArray *keys = [_allNameDictionary allKeys];
//打印
//按升序进行排序(A B C D……)
return [keys sortedArrayUsingSelector:@selector(compare:)];
}
#pragma mark - TableView tableview相关delete和datasource
//引入索引的一个代理方法
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
NSArray *keys = [self getCityDictAllKeys];//获得所有的key值
return keys;
}
//section上的标题(A B C D……Z)
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSArray *keys = [self getCityDictAllKeys];//获得所有的key值(A B C D……Z)
return keys[section];
}
//section的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
NSArray *keys = [self getCityDictAllKeys];//获得所有的key值
return keys.count;
}
//每个section对应的cell的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSArray *keys = [self getCityDictAllKeys];//获得所有的key值
NSString *keyStr = keys[section];//(A B C D……Z)
NSArray *array = [_allNameDictionary objectForKey:keyStr];//所有section下key值所对应的value的值
return array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyFriendSelectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"selectFriend" forIndexPath:indexPath];
NSArray *keys = [self getCityDictAllKeys];//获得所有的key值
NSString *keyStr = keys[indexPath.section];
NSArray *array = [_allNameDictionary objectForKey:keyStr];//所有section下key值所对应的value的值,array就是value值,存放的是model模型
MyFriendModel *model = [array objectAtIndex:indexPath.row];
cell.headImageView.image = [UIImage imageNamed:@"headerImage.jpg"];
cell.nameLabel.text = model.name;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//设置勾
// cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectButton.selected = NO;
for (NSIndexPath *index in _selectIndexs) {
if (index == indexPath) { //改行在选择的数组里面有记录
cell.selectButton.selected =YES; //打勾
break;
}
}
return cell;
}
//点击每个cell触发的事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//cell的多选操作
//获取到点击的cell
MyFriendSelectTableViewCell *cell = (MyFriendSelectTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
if (cell.selectButton.selected==YES) { //如果为选中状态
cell.selectButton.selected=NO; //切换为未选中
[_selectIndexs removeObject:indexPath]; //数据移除
}else { //未选中
cell.selectButton.selected=YES; //切换为选中
[_selectIndexs addObject:indexPath]; //添加索引数据到数组
}
// MyFriendSelectTableViewCell *cell = (MyFriendSelectTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
// cell.accessoryType = UITableViewCellAccessoryCheckmark;
// FriendMessageViewController *message = [[FriendMessageViewController alloc]init];
// NSArray *keys = [self getCityDictAllKeys];//获得所有的key值
// NSString *keyStr = keys[indexPath.section];
// NSArray *array = [_allNameDictionary objectForKey:keyStr];//所有section下key值所对应的value的值,array就是value值,存放的是model模型
// MyFriendModel *model = [array objectAtIndex:indexPath.row];
// message.userId = model.name;
// [self.navigationController pushViewController:message animated:YES];
// FriendInfoViewController *vc = [[FriendInfoViewController alloc] init];
// [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - UISearchBar - delegate
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
BOOL isSearch = YES;//有编辑内容时为YES
if (searchText.length <= 0) {
isSearch = NO;//被清空时为NO
}
NSString *searchStr = _searchBar.text;
[_searchArr removeAllObjects];//清空searchDataArr,防止显示之前搜索的结果内容
//把这个文本与数据源进行比较
//把数据源中类似的数据取出,存入searchDataArr
for (NSInteger i= 0;i < _allNewFirendDataArray.count; i ++){
MyFriendModel *model = _allNewFirendDataArray[i];
searchStr = [searchStr lowercaseString];//转换成小写
BOOL isHas = [model.name hasPrefix:searchStr];//判断model.name是否以字符串searchStr开头
if(isHas){
[_searchArr addObject:model];
}else{
isHas = [model.namePinyin hasPrefix:searchStr];
if (isHas) {
[_searchArr addObject:model];
}
}
}
if (searchStr.length>0) {
[_allNameDictionary removeAllObjects];
//遍历
for (MyFriendModel *model in _searchArr) {
NSMutableArray *letterArr = _allNameDictionary[model.nameLetter];
//判断数组里是否有元素,如果为nil,则实例化该数组,
if (letterArr == nil) {
letterArr = [[NSMutableArray alloc] init];
[_allNameDictionary setObject:letterArr forKey:model.nameLetter];
}
[letterArr addObject:model];
}
}else{
//遍历
for (MyFriendModel *model in _allNewFirendDataArray) {
NSMutableArray *letterArr = _allNameDictionary[model.nameLetter];
//判断数组里是否有元素,如果为nil,则实例化该数组,
if (letterArr == nil) {
letterArr = [[NSMutableArray alloc] init];
[_allNameDictionary setObject:letterArr forKey:model.nameLetter];
}
[letterArr addObject:model];
}
}
[_tableView reloadData];
}
#pragma mark - ButtonAction 按钮事件
- (void)onHideKeyboard{
[_searchBar resignFirstResponder];
}
- (void)leftSelectedEvent:(id)sender{
[self.navigationController popViewControllerAnimated:YES];
}
- (void)rightSelectedEvent:(id)sender{
// [[NIMSDK sharedSDK].userManager deleteFriend:@"111111" completion:nil]; //删除好友
}