iOS实现简单的抽屉式侧栏——MMDraweController的使用

新建项目

新建一个名叫MMDrawerControllerDemo的项目,language选择Swift,storyboard示意图如下,中间是一个Button push 到某一个页面,将起始页面的title改为Home。

新建一个文件,选择cocoa touch class,命名为SecondViewController

在storyboard中选择右边的ViewController,在identity inspector中将其class设置为SecondViewController,同时设置一个storyboard ID

导入MMDrawerController

从终端进入工程目录:
touch Podfile(创建Podfile文件,该文件用于描述第三方库信息)
vim Podfile (编辑Podfile文件,编辑第三方库信息)
Podfile编辑界面输入以下内容:
platform :ios
pod 'MMDrawerController', '~> 0.5.7'
完成后先按esc键退出编辑  —>  再输入 :wq ——>  回车,输入以下命令完成安装:
pod install --verbose --no-repo-update (安装第三方库)
安装完成后会出现如下提示,下次打开工程时使用workspace文件打开:
由于这里使用的是由OC编写的第三方库,因此还要使用一个桥接文件来连接导入的库和工程,新建一个MMDrawerControllerDemo-Bridge-Header.h文件:
 在工程的设置目录下填写桥接文件的完整路径:
 

创建侧栏

新建一个cocoa touch class文件,命名为LeftDrawerViewController,同时新建一个LeftDrawerTableViewCell。

其实可以同时在主页的左右两边各添加一个侧栏,但是我这里仅仅是在左侧添加。
接下来,在viewDidLoad()方法之前加上:

var tableView: UITableView!

在viewDidLoad()方法的最后加上:

tableView = UITableView(frame: CGRectZero, style: .Plain)
tableView.frame = CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: self.view.bounds.size.height)
tableView.delegate = self
tableView.dataSource = self
view.addSubview(tableView)
tableView.registerNib(UINib(nibName: "LeftDrawerTableViewCell", bundle: nil), forCellReuseIdentifier: "LeftDrawerTableViewCell")

在class的外部加上以下代码:

// MARK: - table view delegate, table view datasource
extension LeftDrawerViewController: UITableViewDataSource, UITableViewDelegate {

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("LeftDrawerTableViewCell", forIndexPath: indexPath) as! LeftDrawerTableViewCell
        return cell
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
}

 

posted on 2016-03-07 08:05  罗小夕  阅读(491)  评论(0编辑  收藏  举报

导航