1.新建RootViewController类
- //@H_301_31@
- //RootViewController.swift@H_301_31@
- //UITableViewDemo@H_301_31@
- //@H_301_31@
- //Createdby赵超on14-6-21.@H_301_31@
- //Copyright(c)2014年赵超.Allrightsreserved.@H_301_31@
- @H_301_31@
- importUIKit@H_301_31@
- classRootViewController:UIViewController,UITableViewDelegate,UITableViewDataSource{@H_301_31@
- vartableView:UITableView?@H_301_31@ @H_403_86@ varitems=["武汉","上海","北京","深圳","广州","重庆","香港","台海","天津"]@H_301_31@
- varleftBtn:UIButton?@H_301_31@ @H_403_86@ varrightButtonItem:UIBarButtonItem?@H_301_31@
- @H_301_31@ @H_403_86@ overridefuncviewDidLoad(){@H_301_31@
- super.viewDidLoad()@H_301_31@ @H_403_86@ initView()@H_301_31@
- setupRightBarButtonItem()@H_301_31@ @H_403_86@ setupLeftBarButtonItem()@H_301_31@
- self.leftBtn!.userInteractionEnabled=true@H_301_31@
- //Doanyadditionalsetupafterloadingtheview.@H_301_31@ @H_403_86@ }@H_301_31@
- funcinitView(){@H_301_31@
- //初始化tableView的数据@H_301_31@ @H_403_86@ self.tableView=UITableView(frame:self.view.frame,style:UITableViewStyle.Plain)@H_301_31@
- //设置tableView的数据源@H_301_31@
- self.tableView!.dataSource=self@H_301_31@
- //设置tableView的委托@H_301_31@
- self.tableView!.delegate=self.tableView!.registerClass(UITableViewCell.self,forCellReuseIdentifier:"cell")@H_301_31@
- self.view.addSubview(self.tableView!)@H_301_31@
- //加左边按钮@H_301_31@ @H_403_86@ funcsetupLeftBarButtonItem()@H_301_31@
- {@H_301_31@
- self.leftBtn=UIButton.buttonWithType(UIButtonType.Custom)as?UIButton@H_301_31@
- self.leftBtn!.frame=CGRectMake(0,0,50,40)@H_301_31@
- self.leftBtn?.setTitleColor(UIColor.redColor(),0); background-color:inherit">forState:UIControlState.Normal)@H_301_31@
- self.leftBtn?.setTitle("Edit",0); background-color:inherit">forState:UIControlState.Normal)@H_301_31@
- self.leftBtn!.tag=100@H_301_31@
- false@H_301_31@
- self.leftBtn?.addTarget(self,0); background-color:inherit">action:"leftBarButtonItemClicked",0); background-color:inherit">forControlEvents:UIControlEvents.TouchUpInside)@H_301_31@
- varbarButtonItem=UIBarButtonItem(customView:self.leftBtn)@H_301_31@
- self.navigationItem!.leftBarButtonItem=barButtonItem@H_301_31@
- }@H_301_31@
- //左边按钮事件@H_301_31@
- funcleftBarButtonItemClicked()@H_301_31@ @H_403_86@ {@H_301_31@
- println("leftBarButton")@H_301_31@
- if(self.leftBtn!.tag==100)@H_301_31@
- self.tableView?.setEditing(true,0); background-color:inherit">animated:true)@H_301_31@
- self.leftBtn!.tag=200@H_301_31@
- self.leftBtn?.setTitle("Done",0); background-color:inherit">//将增加按钮设置不能用@H_301_31@
- self.rightButtonItem!.enabled=false@H_301_31@
- else@H_301_31@
- //恢复增加按钮@H_301_31@
- false,153); font-weight:bold; background-color:inherit">self.leftBtn!.tag=100@H_301_31@
- //加右边按钮@H_301_31@ @H_403_86@ funcsetupRightBarButtonItem()@H_301_31@
- self.rightButtonItem=UIBarButtonItem(title:"Add",0); background-color:inherit">style:UIBarButtonItemStyle.Plain,0); background-color:inherit">target:"rightBarButtonItemClicked")@H_301_31@
- self.navigationItem!.rightBarButtonItem=self.rightButtonItem@H_301_31@
- //增加事件@H_301_31@
- funcrightBarButtonItemClicked()@H_301_31@ @H_403_86@ varrow=self.items.count@H_301_31@
- varindexPath=NSIndexPath(forRow:row,inSection:0)@H_301_31@
- self.items.append("杭州")@H_301_31@
- self.tableView?.insertRowsAtIndexPaths([indexPath],0); background-color:inherit">withRowAnimation:UITableViewRowAnimation.Left)@H_301_31@
- overridefuncdidReceiveMemoryWarning(){@H_301_31@
- super.didReceiveMemoryWarning()@H_301_31@
- //DispoSEOfanyresourcesthatcanberecreated.@H_301_31@
- //总行数@H_301_31@
- functableView(tableView:UITableView!,numberOfRowsInSectionsection:Int)->Int{@H_301_31@
- return//加载数据@H_301_31@ @H_403_86@ functableView(tableView:UITableView!,cellForRowAtIndexPathindexPath:NSIndexPath!)->UITableViewCell!{@H_301_31@
- letcell=tableView.dequeueReusableCellWithIdentifier("cell",0); background-color:inherit">forIndexPath:indexPath)asUITableViewCell@H_301_31@
- varrow=indexPath.rowasInt@H_301_31@ @H_403_86@ cell.textLabel.text=self.items[row]@H_301_31@
- cell.imageView.image=UIImage(named:"green.png")@H_301_31@
- returncell;@H_301_31@
- //删除一行@H_301_31@
- editingStyle:UITableViewCellEditingStyle,forRowAtIndexPathindexPath:NSIndexPath!){@H_301_31@ @H_403_86@ varindex=indexPath.rowasInt@H_301_31@
- self.items.removeAtIndex(index)@H_301_31@
- self.tableView?.deleteRowsAtIndexPaths([indexPath],0); background-color:inherit">withRowAnimation:UITableViewRowAnimation.Top)@H_301_31@
- NSLog("删除\(indexPath.row)")@H_301_31@
- //选择一行@H_301_31@
- indexPath:NSIndexPath!){@H_301_31@
- letalert=UIAlertView()@H_301_31@ @H_403_86@ alert.title="提示"@H_301_31@
- alert.message="你选择的是\(self.items[indexPath.row])"@H_301_31@ @H_403_86@ alert.addButtonWithTitle("Ok")@H_301_31@
- alert.show()@H_301_31@ @H_403_86@ }@H_301_31@ @H_502_995@
2.APPDelegate.swift调用
- //@H_301_31@
- //AppDelegate.swift@H_301_31@
- //UITableViewDemo@H_301_31@
- //@H_301_31@
- //Createdby赵超on14-6-21.@H_301_31@
- //Copyright(c)2014年赵超.Allrightsreserved.@H_301_31@
- @H_301_31@
- importUIKit@H_301_31@
- @UIApplicationMain@H_301_31@
- AppDelegate:UIResponder,UIApplicationDelegate{@H_301_31@
- @H_301_31@
- window:UIWindow?@H_301_31@
- funcapplication(application:UIApplication,didFinishLaunchingWithOptionslaunchOptions:NSDictionary?)->Bool{@H_301_31@
- self.window=UIWindow(frame:UIScreen.mainScreen().bounds)@H_301_31@
- //Overridepointforcustomizationafterapplicationlaunch.@H_301_31@ @H_403_86@ varrootView=RootViewController()@H_301_31@
- varnav=UINavigationController(rootViewController:rootView)@H_301_31@
- self.window!.rootViewController=nav;@H_301_31@
- self.window!.backgroundColor=UIColor.whiteColor()@H_301_31@
- self.window!.makeKeyAndVisible()@H_301_31@
- true@H_301_31@ @H_403_86@ }@H_301_31@
- funcapplicationWillResignActive(application:UIApplication){@H_301_31@
- //Sentwhentheapplicationisabouttomovefromactivetoinactivestate.Thiscanoccurforcertaintypesoftemporaryinterruptions(suchasanincomingphonecallorSMSmessage)orwhentheuserquitstheapplicationanditbeginsthetransitiontothebackgroundstate.@H_301_31@
- //UsethismethodtopauSEOngoingtasks,disabletimers,andthrottledownOpenGLESframerates.Gamesshouldusethismethodtopausethegame.@H_301_31@
- }@H_301_31@
- funcapplicationDidEnterBackground(application:UIApplication){@H_301_31@
- //Usethismethodtoreleasesharedresources,saveuserdata,invalidatetimers,andstoreenoughapplicationstateinformationtorestoreyourapplicationtoitscurrentstateincaseitisterminatedlater.@H_301_31@
- //Ifyourapplicationsupportsbackgroundexecution,thismethodiscalledinsteadofapplicationWillTerminate:whentheuserquits.@H_301_31@ @H_403_86@ funcapplicationWillEnterForeground(application:UIApplication){@H_301_31@
- //Calledaspartofthetransitionfromthebackgroundtotheinactivestate;hereyoucanundomanyofthechangesmadeonenteringthebackground.@H_301_31@ @H_403_86@ funcapplicationDidBecomeActive(application:UIApplication){@H_301_31@
- //Restartanytasksthatwerepaused(ornotyetstarted)whiletheapplicationwasinactive.IftheapplicationwasprevIoUslyinthebackground,optionallyrefreshtheuserinterface.@H_301_31@ @H_403_86@ funcapplicationWillTerminate(application:UIApplication){@H_301_31@
- //Calledwhentheapplicationisabouttoterminate.Savedataifappropriate.SeealsoapplicationDidEnterBackground:.@H_301_31@
- }@H_301_31@ @H_502_995@
3.效果
完整工程代码 :https://github.com/whzhaochao/IOS-Swift-UITableViewDemo