微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐