1.新建RootViewController类
- //
- //RootViewController.swift
- //UITableViewDemo
- //
- //Createdby赵超on14-6-21.
- //copyright(c)2014年赵超.Allrightsreserved.
- importUIKit
- classRootViewController:UIViewController,UITableViewDelegate,UITableViewDataSource{
- vartableView:UITableView?
- varitems=["武汉","上海","北京","深圳","广州","重庆","香港","台海","天津"]
- varleftBtn:UIButton?
- varrightButtonItem:UIBarButtonItem?
- overridefuncviewDidLoad(){
- super.viewDidLoad()
- initView()
- setupRightBarButtonItem()
- setupLeftBarButtonItem()
- self.leftBtn!.userInteractionEnabled=true
- //Doanyadditionalsetupafterloadingtheview.
- }
- funcinitView(){
- //初始化tableView的数据
- self.tableView=UITableView(frame:self.view.frame,style:UITableViewStyle.Plain)
- //设置tableView的数据源
- self.tableView!.dataSource=self
- //设置tableView的委托
- self.tableView!.delegate=self.tableView!.registerClass(UITableViewCell.self,forCellReuseIdentifier:"cell")
- self.view.addSubview(self.tableView!)
- //加左边按钮
- funcsetupLeftBarButtonItem()
- {
- self.leftBtn=UIButton.buttonWithType(UIButtonType.Custom)as?UIButton
- self.leftBtn!.frame=CGRectMake(0,0,50,40)
- self.leftBtn?.setTitleColor(UIColor.redColor(),0); background-color:inherit">forState:UIControlState.normal)
- self.leftBtn?.setTitle("Edit",0); background-color:inherit">forState:UIControlState.normal)
- self.leftBtn!.tag=100
- false
- self.leftBtn?.addTarget(self,0); background-color:inherit">action:"leftBarButtonItemClicked",0); background-color:inherit">forControlEvents:UIControlEvents.TouchUpInside)
- varbarButtonItem=UIBarButtonItem(customView:self.leftBtn)
- self.navigationItem!.leftBarButtonItem=barButtonItem
- }
- //左边按钮事件
- funcleftBarButtonItemClicked()
- {
- println("leftBarButton")
- if(self.leftBtn!.tag==100)
- self.tableView?.setEditing(true,0); background-color:inherit">animated:true)
- self.leftBtn!.tag=200
- self.leftBtn?.setTitle("Done",0); background-color:inherit">//将增加按钮设置不能用
- self.rightButtonItem!.enabled=false
- else
- //恢复增加按钮
- false,153); font-weight:bold; background-color:inherit">self.leftBtn!.tag=100
- //加右边按钮
- funcsetupRightBarButtonItem()
- self.rightButtonItem=UIBarButtonItem(title:"Add",0); background-color:inherit">style:UIBarButtonItemStyle.Plain,0); background-color:inherit">target:"rightBarButtonItemClicked")
- self.navigationItem!.rightBarButtonItem=self.rightButtonItem
- //增加事件
- funcrightBarButtonItemClicked()
- varrow=self.items.count
- varindexPath=NSIndexPath(forRow:row,inSection:0)
- self.items.append("杭州")
- self.tableView?.insertRowsAtIndexPaths([indexPath],0); background-color:inherit">withRowAnimation:UITableViewRowAnimation.Left)
- overridefuncdidReceiveMemoryWarning(){
- super.didReceiveMemoryWarning()
- //dispoSEOfanyresourcesthatcanberecreated.
- //总行数
- functableView(tableView:UITableView!,numberOfRowsInSectionsection:Int)->Int{
- return//加载数据
- functableView(tableView:UITableView!,cellForRowAtIndexPathindexPath:NSIndexPath!)->UITableViewCell!{
- letcell=tableView.dequeueReusableCellWithIdentifier("cell",0); background-color:inherit">forIndexPath:indexPath)asUITableViewCell
- varrow=indexPath.rowasInt
- cell.textLabel.text=self.items[row]
- cell.imageView.image=UIImage(named:"green.png")
- returncell;
- //删除一行
- editingStyle:UITableViewCellEditingStyle,forRowAtIndexPathindexPath:NSIndexPath!){
- varindex=indexPath.rowasInt
- self.items.removeAtIndex(index)
- self.tableView?.deleteRowsAtIndexPaths([indexPath],0); background-color:inherit">withRowAnimation:UITableViewRowAnimation.Top)
- NSLog("删除\(indexPath.row)")
- //选择一行
- indexPath:NSIndexPath!){
- letalert=UIAlertView()
- alert.title="提示"
- alert.message="你选择的是\(self.items[indexPath.row])"
- alert.addButtonWithTitle("Ok")
- alert.show()
- }
2.APPDelegate.swift调用
- //
- //AppDelegate.swift
- //UITableViewDemo
- //
- //Createdby赵超on14-6-21.
- //copyright(c)2014年赵超.Allrightsreserved.
- importUIKit
- @UIApplicationMain
- AppDelegate:UIResponder,UIApplicationDelegate{
- window:UIWindow?
- funcapplication(application:UIApplication,didFinishLaunchingWithOptionslaunchOptions:NSDictionary?)->Bool{
- self.window=UIWindow(frame:UIScreen.mainScreen().bounds)
- //Overridepointforcustomizationafterapplicationlaunch.
- varrootView=RootViewController()
- varnav=UINavigationController(rootViewController:rootView)
- self.window!.rootViewController=nav;
- self.window!.backgroundColor=UIColor.whiteColor()
- self.window!.makeKeyAndVisible()
- true
- }
- funcapplicationWillResignActive(application:UIApplication){
- //Sentwhentheapplicationisabouttomovefromactivetoinactivestate.Thiscanoccurforcertaintypesoftemporaryinterruptions(suchasanincomingphonecallorSMSmessage)orwhentheuserquitstheapplicationanditbeginsthetransitiontothebackgroundstate.
- //UsethismethodtopauSEOngoingtasks,disabletimers,andthrottledownopenGLESframerates.Gamesshouldusethismethodtopausethegame.
- }
- funcapplicationDidEnterBackground(application:UIApplication){
- //Usethismethodtoreleasesharedresources,saveuserdata,invalidatetimers,andstoreenoughapplicationstateinformationtorestoreyourapplicationtoitscurrentstateincaseitisterminatedlater.
- //Ifyourapplicationsupportsbackgroundexecution,thismethodiscalledinsteadofapplicationWillTerminate:whentheuserquits.
- funcapplicationWillEnterForeground(application:UIApplication){
- //Calledaspartofthetransitionfromthebackgroundtotheinactivestate;hereyoucanundomanyofthechangesmadeonenteringthebackground.
- funcapplicationDidBecomeActive(application:UIApplication){
- //Restartanytasksthatwerepaused(ornotyetstarted)whiletheapplicationwasinactive.IftheapplicationwasprevIoUslyinthebackground,optionallyrefreshtheuserinterface.
- funcapplicationWillTerminate(application:UIApplication){
- //Calledwhentheapplicationisabouttoterminate.Savedataifappropriate.SeealsoapplicationDidEnterBackground:.
- }
3.效果
完整工程代码 :https://github.com/whzhaochao/IOS-Swift-UITableViewDemo
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。