swift_UITableView详解

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

1.新建RootViewController类

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

2.APPDelegate.swift调用

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

3.效果

完整工程代码 :https://github.com/whzhaochao/IOS-Swift-UITableViewDemo

猜你在找的Swift相关文章