【swift_3】swift之UITableView和UINavigation视图控制器

来源:互联网 发布:ubuntu提示权限不够 编辑:程序博客网 时间:2024/06/02 22:45

Demo:链接: http://download.csdn.net/download/riven_wn/9401961

AppDelegate

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. var window: UIWindow?  
  2.   
  3.   
  4.     func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool  
  5.     {  
  6.         var VC = ViewController()  
  7.         var nav = UINavigationController(rootViewController: VC)  
  8.         self.window = UIWindow(frame: UIScreen.mainScreen().bounds)  
  9.         self.window!.rootViewController = nav  
  10.         self.window?.backgroundColor = UIColor.whiteColor()  
  11.         self.window?.makeKeyAndVisible()  
  12.      
  13.         return true  
  14.     }  

ViewController

引入代理,注意这里在完成dataSource的几个方法之前,UITableViewDataSource会报错,完成之后就好了

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource  

定义tableView 和 数据源

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. var _tableView:UITableView?  
  2. var _dataSource = ["星期一","星期二","星期三","星期四","星期五","星期六","星期日"]  
  3.   
  4. override func viewDidLoad()  
  5. {  
  6.     super.viewDidLoad()  
  7.       
  8.     //配置导航栏  
  9.     self.navigationItem.title = "单元格滑动可删除"  
  10.     var leftBtn = UIBarButtonItem(title: "Add", style: UIBarButtonItemStyle.Plain, targetself, action:"add")  
  11.     var rightBtn = UIBarButtonItem(title: "Move", style: UIBarButtonItemStyle.Plain, targetself, action"edit:")  
  12.     rightBtn.tag = 100  
  13.     self.navigationItem.leftBarButtonItem = leftBtn  
  14.     self.navigationItem.rightBarButtonItem = rightBtn  
  15.       
  16.     //配置表  
  17.     _tableView = UITableView(frame: self.view!.bounds)  
  18.     _tableView?.delegate = self  
  19.     _tableView?.dataSource = self  
  20.     self.view .addSubview(_tableView!)  
  21. }  

导航按钮点击事件

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //导航按钮点击事件  
  2.     func add()  
  3.     {  
  4.         println("add sth")  
  5.         _dataSource .append("一周就七天")  
  6.         _tableView?.reloadData()  
  7.     }  
  8.       
  9.     func edit(rightBtn:UIBarButtonItem)  
  10.     {  
  11.         println("edit tableView")  
  12.         if(rightBtn.tag == 100)  
  13.         {  
  14.             _tableView?.setEditing(true, animatedtrue)  
  15.             rightBtn.tag = 200  
  16.             rightBtn.title = "done"  
  17.         }  
  18.         else  
  19.         {  
  20.             _tableView?.setEditing(false, animatedtrue)  
  21.             rightBtn.tag = 100  
  22.             rightBtn.title = "edit"  
  23.         }  
  24.     }  

UITableViewDataSource UITableViewDelegate

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //UITableViewDataSource UITableViewDelegate  
  2.     //行数  
  3.     func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int  
  4.     {  
  5.         return _dataSource.count  
  6.       
  7.     }  
  8.       
  9.     //单元格内容  
  10.     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell  
  11.     {  
  12.         let  identifier = "Cell"  
  13.         var cell = tableView.dequeueReusableCellWithIdentifier(identifier)as? UITableViewCell;  
  14.         if(cell == nil)  
  15.         {  
  16.             cell = UITableViewCell(style:UITableViewCellStyle.Default, reuseIdentifier: identifier)  
  17.         }  
  18.         cell?.textLabel?.text = _dataSource[indexPath.row]  
  19.         return cell!  
  20.     }  
  21.       
  22.     //是否允许打开编辑状态 默认是 可以不写  
  23.     func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool  
  24.     {  
  25.         return true  
  26.     }  
  27.       
  28.       
  29.     func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle  
  30.     {  
  31.         //允许滑动删除  
  32.         if(_tableView?.editing == false)  
  33.         {  
  34.             return UITableViewCellEditingStyle.Delete  
  35.         }  
  36.         //移动单元格  
  37.         else  
  38.         {  
  39.             return UITableViewCellEditingStyle.None  
  40.         }  
  41.     }  
  42.       
  43.     //删除某行  
  44.     func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)  
  45.     {  
  46.         _dataSource.removeAtIndex(indexPath.row)  
  47.         _tableView?.reloadData()  
  48.     }  
  49.       
  50.     //允许移动某行  
  51.     func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool  
  52.     {  
  53.         return true  
  54.     }  
  55.       
  56.     //移动  
  57.     func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath)  
  58.     {  
  59.         _tableView?.moveRowAtIndexPath(sourceIndexPath, toIndexPath: destinationIndexPath)  
  60.         var itemToMove = _dataSource[sourceIndexPath.row]  
  61.         //在这里找不到数组exchange的方法, 所以采用先移除后添加来实现交换位置  
  62.         _dataSource.removeAtIndex(sourceIndexPath.row)  
  63.         _dataSource.insert(itemToMove, atIndex: destinationIndexPath.row)  
  64.     }  
  65.   
  66.     //cell点击事件  
  67.     func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)  
  68.     {  
  69.         switch indexPath.row  
  70.         {  
  71.         case 0:  
  72.             self.navigationController?.pushViewController(FirstViewController(), animatedtrue)  
  73.   
  74.         case 1:  
  75.             self .presentViewController(SecondViewController(), animatedtrue, completion: nil)  
  76.               
  77.         case 2:  
  78.             self.navigationController?.pushViewController(ThirdViewController(), animatedtrue)  
  79.         default:  
  80.             var alert = UIAlertView(title: "提示", message"不是前三", delegate: nil, cancelButtonTitle"cancel")  
  81.             alert.show()  
  82.         }  
  83.     }  

SecondViewController 

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. override func viewDidLoad() {  
  2.         super.viewDidLoad()  
  3.   
  4.         self.navigationItem.title = "Second"  
  5.         self.view.backgroundColor = UIColor.redColor()  
  6.           
  7.         var button = UIButton(frame: self.view.bounds)  
  8.         button.addTarget(self, action"buttonClick", forControlEvents: UIControlEvents.TouchUpInside)  
  9.         button.setTitle("点击屏幕返回", forState: UIControlState.Normal)  
  10.         self.view.addSubview(button)  
  11.     }  
  12.   
  13.     func buttonClick()  
  14.     {  
  15.         print("buttonClick")  
  16.         self .dismissViewControllerAnimated(true, completion: nil)  
  17.     }  

ThirdViewController

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. override func viewDidLoad() {  
  2.         super.viewDidLoad()  
  3.   
  4.         self.navigationItem.title = "Third"  
  5.         self.view.backgroundColor = UIColor.greenColor()  
  6.           
  7.         var button = UIButton(frame: self.view.bounds)  
  8.         button.addTarget(self, action"buttonClick", forControlEvents: UIControlEvents.TouchUpInside)  
  9.         button.setTitle("点击屏幕去根视图", forState: UIControlState.Normal)  
  10.         self.view.addSubview(button)  
  11.     }  
  12.       
  13.     func buttonClick()  
  14.     {  
  15.         print("buttonClick")  
  16.         self.navigationController?.popToRootViewControllerAnimated(true)  
  17.     }  
0 0
原创粉丝点击