iOS分组通讯录效果+側滑菜单(MMDrawerController)

时间:2023-03-10 01:44:47
iOS分组通讯录效果+側滑菜单(MMDrawerController)

前言的废话…能够忽略

自从学会了使用Cocoapod,就欲罢不能了!由于太简单太赞了,不用再把源代码粘到project里了!

參见戴维营博客中的解说:Cocoapod 安装以及使用

先上一下效果图,请原谅我手残录的效果不是非常理想,大致就是这个意思iOS分组通讯录效果+側滑菜单(MMDrawerController)

接下来上代码!

1.通讯录

通讯录基本的就是建立索引栏和section的关联,其次是初始化索引栏数据和每一个section的title.关于索引栏数据,假设写接口的小哥人好的话就会直接帮你返回ABCD…假设非常不幸,接口小哥不给你返回索引栏数据,那就得自己处理了!(处理方法兴许再补上,如今先假设接收到了索引栏数据)

(1.)私有成员

 @property(nonatomic,strong)NSArray* bottomTableData;
@property(nonatomic,strong)NSArray* indexData;

(2.)相关函数

    //每一个section 的title
- (UIView *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ return [_indexData objectAtIndex:section];
}
//返回索引栏数据
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ return _indexData;
}
//建立索引栏和section的关联
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title
atIndex:(NSInteger)index{ NSInteger section = [_indexData indexOfObject:title];
return section;
}

2.側滑菜单

(1.)私有成员

@property (weak, nonatomic) IBOutlet UITableView *topTableView;//新的好友,我的粉丝,我的群
@property (nonatomic,strong)BottomTableView* bottomTabelView;//下半部分的通讯录
@property(nonatomic,strong)UILabel* groupNameLbl;
@property(nonatomic,strong)NSArray* topTableData;
@property(nonatomic,strong)NSArray* bottomTableData;
@property(nonatomic,strong)NSArray* indexData;//索引数据,对接接口后依据返回的对应数据进行修改
@property(nonatomic,strong)NSArray* leftMenuData;//側滑菜单

(2.)相关函数

    1>.在viewDidLoad中初始化数据
2>.设置側滑菜单(使用MMDrawerController)! LeftSideDrawerViewController* leftMenuController = [[LeftSideDrawerViewController alloc] init]; leftMenuController.imgData = @[@"allFriend",@"jiaren",@"pengyou",@"tongxue",@"weifenzu"];
/**
注意:每一个页面要用NavigationViewController包一下.我不是在主页面写的側滑菜单,而是在模态窗体里写的.这里的參数须要细致检查,非常easy出现错误,假设參数出现错误,界面效果会有问题的,详细的你能够自己试着修改一下,深刻的理解一下.我这里的self不过一个ViewController,所以须要NavigationViewController再包一层.
*/
self.drawController = [[MMDrawerController alloc] initWithCenterViewController:[[NavigationViewController alloc] initWithRootViewController:self] leftDrawerViewController:leftController]; [_drawController setShowsShadow:NO]; [_drawController setMaximumLeftDrawerWidth:kScreenWidth*4/5]; [_drawController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll]; [_drawController setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll]; _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; UIColor * tintColor = [UIColor colorWithRed:29.0/255.0
green:173.0/255.0
blue:234.0/255.0
alpha:1.0]; [_window setTintColor:tintColor]; _window.rootViewController = _drawController; [_window makeKeyAndVisible]; 3>.初始化数据 _topTableData = @[@[@"新的朋友(0)",@"qunmemberaction"],@[@"我的粉丝(0)",@"qunweiboaction"],@[@"我的群(0)",@"qunmemberaction"]]; _bottomTableData = @[@[@"啊1",@"啊2"],@[@"波波",@"菠菜"],@[@"赫赫"],@[@"校内外助手",@"新人",@"小人",@"昕人"]]; _indexData = @[@"A",@"B",@"H",@"X"]; _leftMenuData = @[@"所有好友(2)",@"家人(0)",@"朋友(0)",@"同学(0)",@"未分组(2)"];
4>.选择左側菜单中某一项,側滑菜单关闭并刷新主页面的数据
a.側滑菜单.m中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ ContactsViewController* contactController = [[ContactsViewController alloc] initWithNibName:@"ContactsViewController" bundle:nil]; self.leftDelegate = contactController; [self.leftDelegate passToContacts:[NSString stringWithFormat:@"%ld",(long)indexPath.row]]; [self.mm_drawerController closeDrawerAnimated:YES completion:nil];
}
b.主页面.m中
- (void)passToContacts:(NSString*)value{
//又一次请求好友通讯录,并刷新tableview
groupIndex = [value intValue];
[self viewDidLoad];
}

3.从主页面返回上一级页面

//更改window的根视图控制器
- (void)pressCancleBtn:(id)sender{ TabbarViewController* tabbarController = [[TabbarViewController alloc]init]; tabbarController.selectedIndex = 4;
self.window.rootViewController = tabbarController; [self.window makeKeyAndVisible]; }

最后的啰嗦

由于是菜鸟,所以希望大家多多提意见,共同进步!最后附代码地址:MollyMmm的github