1. 程式人生 > >iOS分組通訊錄效果+側滑菜單(MMDrawerController)

iOS分組通訊錄效果+側滑菜單(MMDrawerController)

ntc 函數 處理方法 cocoa javascrip enter title clas uiwindow

前言的廢話…能夠忽略
自從學會了使用Cocoapod,就欲罷不能了!由於太簡單太贊了,不用再把源代碼粘到project裏了!
參見戴維營博客中的解說:Cocoapod 安裝以及使用
先上一下效果圖,請原諒我手殘錄的效果不是非常理想,大致就是這個意思技術分享

接下來上代碼!

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

iOS分組通訊錄效果+側滑菜單(MMDrawerController)