【swift_3】swift之UITableView和UINavigation视图控制器
来源:互联网 发布:ubuntu提示权限不够 编辑:程序博客网 时间:2024/06/02 22:45
Demo:链接: http://download.csdn.net/download/riven_wn/9401961
AppDelegate
- var window: UIWindow?
- func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
- {
- var VC = ViewController()
- var nav = UINavigationController(rootViewController: VC)
- self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
- self.window!.rootViewController = nav
- self.window?.backgroundColor = UIColor.whiteColor()
- self.window?.makeKeyAndVisible()
- return true
- }
ViewController
引入代理,注意这里在完成dataSource的几个方法之前,UITableViewDataSource会报错,完成之后就好了
- class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource
定义tableView 和 数据源
- var _tableView:UITableView?
- var _dataSource = ["星期一","星期二","星期三","星期四","星期五","星期六","星期日"]
- override func viewDidLoad()
- {
- super.viewDidLoad()
- //配置导航栏
- self.navigationItem.title = "单元格滑动可删除"
- var leftBtn = UIBarButtonItem(title: "Add", style: UIBarButtonItemStyle.Plain, target: self, action:"add")
- var rightBtn = UIBarButtonItem(title: "Move", style: UIBarButtonItemStyle.Plain, target: self, action: "edit:")
- rightBtn.tag = 100
- self.navigationItem.leftBarButtonItem = leftBtn
- self.navigationItem.rightBarButtonItem = rightBtn
- //配置表
- _tableView = UITableView(frame: self.view!.bounds)
- _tableView?.delegate = self
- _tableView?.dataSource = self
- self.view .addSubview(_tableView!)
- }
导航按钮点击事件
- //导航按钮点击事件
- func add()
- {
- println("add sth")
- _dataSource .append("一周就七天")
- _tableView?.reloadData()
- }
- func edit(rightBtn:UIBarButtonItem)
- {
- println("edit tableView")
- if(rightBtn.tag == 100)
- {
- _tableView?.setEditing(true, animated: true)
- rightBtn.tag = 200
- rightBtn.title = "done"
- }
- else
- {
- _tableView?.setEditing(false, animated: true)
- rightBtn.tag = 100
- rightBtn.title = "edit"
- }
- }
UITableViewDataSource UITableViewDelegate
- //UITableViewDataSource UITableViewDelegate
- //行数
- func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
- {
- return _dataSource.count
- }
- //单元格内容
- func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
- {
- let identifier = "Cell"
- var cell = tableView.dequeueReusableCellWithIdentifier(identifier)as? UITableViewCell;
- if(cell == nil)
- {
- cell = UITableViewCell(style:UITableViewCellStyle.Default, reuseIdentifier: identifier)
- }
- cell?.textLabel?.text = _dataSource[indexPath.row]
- return cell!
- }
- //是否允许打开编辑状态 默认是 可以不写
- func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool
- {
- return true
- }
- func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle
- {
- //允许滑动删除
- if(_tableView?.editing == false)
- {
- return UITableViewCellEditingStyle.Delete
- }
- //移动单元格
- else
- {
- return UITableViewCellEditingStyle.None
- }
- }
- //删除某行
- func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
- {
- _dataSource.removeAtIndex(indexPath.row)
- _tableView?.reloadData()
- }
- //允许移动某行
- func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool
- {
- return true
- }
- //移动
- func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath)
- {
- _tableView?.moveRowAtIndexPath(sourceIndexPath, toIndexPath: destinationIndexPath)
- var itemToMove = _dataSource[sourceIndexPath.row]
- //在这里找不到数组exchange的方法, 所以采用先移除后添加来实现交换位置
- _dataSource.removeAtIndex(sourceIndexPath.row)
- _dataSource.insert(itemToMove, atIndex: destinationIndexPath.row)
- }
- //cell点击事件
- func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
- {
- switch indexPath.row
- {
- case 0:
- self.navigationController?.pushViewController(FirstViewController(), animated: true)
- case 1:
- self .presentViewController(SecondViewController(), animated: true, completion: nil)
- case 2:
- self.navigationController?.pushViewController(ThirdViewController(), animated: true)
- default:
- var alert = UIAlertView(title: "提示", message: "不是前三", delegate: nil, cancelButtonTitle: "cancel")
- alert.show()
- }
- }
SecondViewController
- override func viewDidLoad() {
- super.viewDidLoad()
- self.navigationItem.title = "Second"
- self.view.backgroundColor = UIColor.redColor()
- var button = UIButton(frame: self.view.bounds)
- button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
- button.setTitle("点击屏幕返回", forState: UIControlState.Normal)
- self.view.addSubview(button)
- }
- func buttonClick()
- {
- print("buttonClick")
- self .dismissViewControllerAnimated(true, completion: nil)
- }
ThirdViewController
- override func viewDidLoad() {
- super.viewDidLoad()
- self.navigationItem.title = "Third"
- self.view.backgroundColor = UIColor.greenColor()
- var button = UIButton(frame: self.view.bounds)
- button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
- button.setTitle("点击屏幕去根视图", forState: UIControlState.Normal)
- self.view.addSubview(button)
- }
- func buttonClick()
- {
- print("buttonClick")
- self.navigationController?.popToRootViewControllerAnimated(true)
- }
0 0
- 【swift_3】swift之UITableView和UINavigation视图控制器
- 【swift_3】swift之UITableView和UINavigation视图控制器
- swift UITableView 表视图
- IOS开发之视图和视图控制器
- IOS UITableView表视图和导航控制器的交互使用
- 视图和视图控制器
- 视图和视图控制器
- 视图和视图控制器
- 视图和视图控制器
- UINavigation 导航控制器
- mvc之控制器,模型和视图
- 表格视图UITableView及控制器UITableViewController
- 表格视图UITableView及控制器UITableViewController
- 表格视图UITableView及控制器UITableViewController
- 【IOS学习】IOS开发之视图和视图控制器
- Swift之UITableView
- swift之UITableView
- iOSDay22之视图控制器
- LINUX多线程
- linux配置redis和phpredis扩展
- bzoj2820&&YY的GCD
- caffe代码阅读8: Data_layers的实现细节(各个数据读取层的实现细节) 2016.3.25-28
- SpringMVC中ModelAndView, Model, ModelMap, Map, String, void的相关内容
- 【swift_3】swift之UITableView和UINavigation视图控制器
- ionic 2 添加第三方的js
- ElasticSearch java API 按照某个字段排序
- Data Binding-初学篇-(1)
- 今日BBC
- linux vi 退出和chow用法
- edgesForExtendedLayout属性解析
- 安卓学习日记20160328
- 并查集(union)模板