swift_UITableView详解

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

@H_502_4@

1.新建RootViewController类@H_502_4@

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

2@H_502_4@.APPDelegate.swift调用

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

3.效果

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

猜你在找的Swift相关文章