swift_UITableView详解

前端之家收集整理的这篇文章主要介绍了swift_UITableView详解前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.新建RootViewController类

[objc]view plaincopy
  1. //
  2. //RootViewController.swift
  3. //UITableViewDemo
  4. //
  5. //Createdby赵超on14-6-21.
  6. //Copyright(c)2014年赵超.Allrightsreserved.
  7. importUIKit
  8. classRootViewController:UIViewController,UITableViewDelegate,UITableViewDataSource{
  9. vartableView:UITableView?
  10. varitems=["武汉","上海","北京","深圳","广州","重庆","香港","台海","天津"]
  11. varleftBtn:UIButton?
  12. varrightButtonItem:UIBarButtonItem?
  13. overridefuncviewDidLoad(){
  14. super.viewDidLoad()
  15. initView()
  16. setupRightBarButtonItem()
  17. setupLeftBarButtonItem()
  18. self.leftBtn!.userInteractionEnabled=true
  19. //Doanyadditionalsetupafterloadingtheview.
  20. }
  21. funcinitView(){
  22. //初始化tableView的数据
  23. self.tableView=UITableView(frame:self.view.frame,style:UITableViewStyle.Plain)
  24. //设置tableView的数据源
  25. self.tableView!.dataSource=self
  26. //设置tableView的委托
  27. self.tableView!.delegate=self.tableView!.registerClass(UITableViewCell.self,forCellReuseIdentifier:"cell")
  28. self.view.addSubview(self.tableView!)
  29. //加左边按钮
  30. funcsetupLeftBarButtonItem()
  31. {
  32. self.leftBtn=UIButton.buttonWithType(UIButtonType.Custom)as?UIButton
  33. self.leftBtn!.frame=CGRectMake(0,0,50,40)
  34. self.leftBtn?.setTitleColor(UIColor.redColor(),0); background-color:inherit">forState:UIControlState.Normal)
  35. self.leftBtn?.setTitle("Edit",0); background-color:inherit">forState:UIControlState.Normal)
  36. self.leftBtn!.tag=100
  37. false
  38. self.leftBtn?.addTarget(self,0); background-color:inherit">action:"leftBarButtonItemClicked",0); background-color:inherit">forControlEvents:UIControlEvents.TouchUpInside)
  39. varbarButtonItem=UIBarButtonItem(customView:self.leftBtn)
  40. self.navigationItem!.leftBarButtonItem=barButtonItem
  41. }
  42. //左边按钮事件
  43. funcleftBarButtonItemClicked()
  44. {
  45. println("leftBarButton")
  46. if(self.leftBtn!.tag==100)
  47. self.tableView?.setEditing(true,0); background-color:inherit">animated:true)
  48. self.leftBtn!.tag=200
  49. self.leftBtn?.setTitle("Done",0); background-color:inherit">//将增加按钮设置不能用
  50. self.rightButtonItem!.enabled=false
  51. else
  52. //恢复增加按钮
  53. false,153); font-weight:bold; background-color:inherit">self.leftBtn!.tag=100
  54. //加右边按钮
  55. funcsetupRightBarButtonItem()
  56. self.rightButtonItem=UIBarButtonItem(title:"Add",0); background-color:inherit">style:UIBarButtonItemStyle.Plain,0); background-color:inherit">target:"rightBarButtonItemClicked")
  57. self.navigationItem!.rightBarButtonItem=self.rightButtonItem
  58. //增加事件
  59. funcrightBarButtonItemClicked()
  60. varrow=self.items.count
  61. varindexPath=NSIndexPath(forRow:row,inSection:0)
  62. self.items.append("杭州")
  63. self.tableView?.insertRowsAtIndexPaths([indexPath],0); background-color:inherit">withRowAnimation:UITableViewRowAnimation.Left)
  64. overridefuncdidReceiveMemoryWarning(){
  65. super.didReceiveMemoryWarning()
  66. //DispoSEOfanyresourcesthatcanberecreated.
  67. //总行数
  68. functableView(tableView:UITableView!,numberOfRowsInSectionsection:Int)->Int{
  69. return//加载数据
  70. functableView(tableView:UITableView!,cellForRowAtIndexPathindexPath:NSIndexPath!)->UITableViewCell!{
  71. letcell=tableView.dequeueReusableCellWithIdentifier("cell",0); background-color:inherit">forIndexPath:indexPath)asUITableViewCell
  72. varrow=indexPath.rowasInt
  73. cell.textLabel.text=self.items[row]
  74. cell.imageView.image=UIImage(named:"green.png")
  75. returncell;
  76. //删除一行
  77. editingStyle:UITableViewCellEditingStyle,forRowAtIndexPathindexPath:NSIndexPath!){
  78. varindex=indexPath.rowasInt
  79. self.items.removeAtIndex(index)
  80. self.tableView?.deleteRowsAtIndexPaths([indexPath],0); background-color:inherit">withRowAnimation:UITableViewRowAnimation.Top)
  81. NSLog("删除\(indexPath.row)")
  82. //选择一行
  83. indexPath:NSIndexPath!){
  84. letalert=UIAlertView()
  85. alert.title="提示"
  86. alert.message="你选择的是\(self.items[indexPath.row])"
  87. alert.addButtonWithTitle("Ok")
  88. alert.show()
  89. }

2.APPDelegate.swift调用

[objc]view plaincopy
  1. //
  2. //AppDelegate.swift
  3. //UITableViewDemo
  4. //
  5. //Createdby赵超on14-6-21.
  6. //Copyright(c)2014年赵超.Allrightsreserved.
  7. importUIKit
  8. @UIApplicationMain
  9. AppDelegate:UIResponder,UIApplicationDelegate{
  10. window:UIWindow?
  11. funcapplication(application:UIApplication,didFinishLaunchingWithOptionslaunchOptions:NSDictionary?)->Bool{
  12. self.window=UIWindow(frame:UIScreen.mainScreen().bounds)
  13. //Overridepointforcustomizationafterapplicationlaunch.
  14. varrootView=RootViewController()
  15. varnav=UINavigationController(rootViewController:rootView)
  16. self.window!.rootViewController=nav;
  17. self.window!.backgroundColor=UIColor.whiteColor()
  18. self.window!.makeKeyAndVisible()
  19. true
  20. }
  21. funcapplicationWillResignActive(application:UIApplication){
  22. //Sentwhentheapplicationisabouttomovefromactivetoinactivestate.Thiscanoccurforcertaintypesoftemporaryinterruptions(suchasanincomingphonecallorSMSmessage)orwhentheuserquitstheapplicationanditbeginsthetransitiontothebackgroundstate.
  23. //UsethismethodtopauSEOngoingtasks,disabletimers,andthrottledownOpenGLESframerates.Gamesshouldusethismethodtopausethegame.
  24. }
  25. funcapplicationDidEnterBackground(application:UIApplication){
  26. //Usethismethodtoreleasesharedresources,saveuserdata,invalidatetimers,andstoreenoughapplicationstateinformationtorestoreyourapplicationtoitscurrentstateincaseitisterminatedlater.
  27. //Ifyourapplicationsupportsbackgroundexecution,thismethodiscalledinsteadofapplicationWillTerminate:whentheuserquits.
  28. funcapplicationWillEnterForeground(application:UIApplication){
  29. //Calledaspartofthetransitionfromthebackgroundtotheinactivestate;hereyoucanundomanyofthechangesmadeonenteringthebackground.
  30. funcapplicationDidBecomeActive(application:UIApplication){
  31. //Restartanytasksthatwerepaused(ornotyetstarted)whiletheapplicationwasinactive.IftheapplicationwasprevIoUslyinthebackground,optionallyrefreshtheuserinterface.
  32. funcapplicationWillTerminate(application:UIApplication){
  33. //Calledwhentheapplicationisabouttoterminate.Savedataifappropriate.SeealsoapplicationDidEnterBackground:.
  34. }

3.效果

完整工程代码 :https://github.com/whzhaochao/IOS-Swift-UITableViewDemo
原文链接:https://www.f2er.com/swift/326168.html

猜你在找的Swift相关文章