加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift_UITableView详解

发布时间:2020-12-14 01:52:56 所属栏目:百科 来源:网络整理
导读:1.新建RootViewController类 [objc] view plaincopy // //RootViewController.swift //UITableViewDemo // //Createdby赵超on14-6-21. //Copyright(c)2014年赵超.Allrightsreserved. importUIKit class RootViewController :UIViewController,UITableViewDel

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读