@H_502_4@
1.新建RootViewController类@H_502_4@
- //@H_502_4@@H_502_4@@H_502_4@
- //RootViewController.swift@H_502_4@@H_502_4@@H_502_4@
- //UITableViewDemo@H_502_4@@H_502_4@@H_502_4@
- //@H_502_4@@H_502_4@@H_502_4@
- //Createdby赵超on14-6-21.@H_502_4@@H_502_4@@H_502_4@
- //Copyright(c)2014年赵超.Allrightsreserved.@H_502_4@@H_502_4@@H_502_4@
- @H_502_4@
- importUIKit@H_502_4@
- class@H_502_4@RootViewController@H_502_4@:UIViewController,UITableViewDelegate,UITableViewDataSource{@H_502_4@@H_502_4@
- vartableView:UITableView?@H_502_4@
- 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@
- varleftBtn@H_502_4@:UIButton?@H_502_4@@H_502_4@
- varrightButtonItem@H_502_4@:UIBarButtonItem?@H_502_4@@H_502_4@
- @H_502_4@
- overridefuncviewDidLoad(){@H_502_4@
- super@H_502_4@.viewDidLoad@H_502_4@()@H_502_4@@H_502_4@
- initView()@H_502_4@
- setupRightBarButtonItem()@H_502_4@
- setupLeftBarButtonItem()@H_502_4@
- 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@
- //Doanyadditionalsetupafterloadingtheview.@H_502_4@@H_502_4@@H_502_4@
- }@H_502_4@
- funcinitView(){@H_502_4@
- //初始化tableView的数据@H_502_4@@H_502_4@@H_502_4@
- 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@
- //设置tableView的数据源@H_502_4@@H_502_4@@H_502_4@
- 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@
- //设置tableView的委托@H_502_4@@H_502_4@@H_502_4@
- 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@
- 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@
- //加左边按钮@H_502_4@@H_502_4@@H_502_4@
- funcsetupLeftBarButtonItem()@H_502_4@
- {@H_502_4@
- 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@
- 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@
- 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@
- 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@
- 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@
- false@H_502_4@@H_502_4@@H_502_4@
- 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@
- varbarButtonItem=UIBarButtonItem(customView:self@H_502_4@.leftBtn@H_502_4@)@H_502_4@@H_502_4@
- self@H_502_4@.navigationItem@H_502_4@!@H_502_4@.leftBarButtonItem@H_502_4@=barButtonItem@H_502_4@@H_502_4@
- }@H_502_4@
- //左边按钮事件@H_502_4@@H_502_4@@H_502_4@
- funcleftBarButtonItemClicked()@H_502_4@
- {@H_502_4@
- println("leftBarButton"@H_502_4@)@H_502_4@@H_502_4@
- 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@
- 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@
- 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@
- 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@
- 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@
- else@H_502_4@@H_502_4@@H_502_4@
- //恢复增加按钮@H_502_4@@H_502_4@@H_502_4@
- 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@
- //加右边按钮@H_502_4@@H_502_4@@H_502_4@
- funcsetupRightBarButtonItem()@H_502_4@
- 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@
- 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@
- //增加事件@H_502_4@@H_502_4@@H_502_4@
- funcrightBarButtonItemClicked()@H_502_4@
- varrow=self@H_502_4@.items@H_502_4@.count@H_502_4@@H_502_4@@H_502_4@
- varindexPath=NSIndexPath(forRow:row,inSection:0@H_502_4@)@H_502_4@@H_502_4@
- self@H_502_4@.items@H_502_4@.append@H_502_4@(@H_502_4@"杭州"@H_502_4@)@H_502_4@@H_502_4@
- 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@
- overridefuncdidReceiveMemoryWarning(){@H_502_4@
- super@H_502_4@.didReceiveMemoryWarning@H_502_4@()@H_502_4@@H_502_4@
- //DispoSEOfanyresourcesthatcanberecreated.@H_502_4@@H_502_4@@H_502_4@
- //总行数@H_502_4@@H_502_4@@H_502_4@
- functableView(tableView:UITableView!,numberOfRowsInSectionsection@H_502_4@:Int)->Int{@H_502_4@@H_502_4@
- return@H_502_4@@H_502_4@//加载数据@H_502_4@@H_502_4@@H_502_4@
- functableView(tableView:UITableView!,cellForRowAtIndexPathindexPath@H_502_4@:NSIndexPath!)->UITableViewCell!{@H_502_4@@H_502_4@
- 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@
- varrow=indexPath.row@H_502_4@asInt@H_502_4@@H_502_4@
- 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@
- 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@
- return@H_502_4@cell;@H_502_4@@H_502_4@
- //删除一行@H_502_4@@H_502_4@@H_502_4@
- editingStyle@H_502_4@:UITableViewCellEditingStyle,forRowAtIndexPath@H_502_4@indexPath@H_502_4@:NSIndexPath!){@H_502_4@@H_502_4@
- varindex=indexPath.row@H_502_4@asInt@H_502_4@@H_502_4@
- self@H_502_4@.items@H_502_4@.removeAtIndex@H_502_4@(index)@H_502_4@@H_502_4@
- 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@
- NSLog("删除\(indexPath.row)"@H_502_4@)@H_502_4@@H_502_4@
- //选择一行@H_502_4@@H_502_4@@H_502_4@
- indexPath@H_502_4@:NSIndexPath!){@H_502_4@@H_502_4@
- letalert=UIAlertView()@H_502_4@
- alert.title@H_502_4@=@H_502_4@"提示"@H_502_4@@H_502_4@@H_502_4@
- alert.message@H_502_4@=@H_502_4@"你选择的是\(self.items[indexPath.row])"@H_502_4@@H_502_4@@H_502_4@
- alert.addButtonWithTitle@H_502_4@(@H_502_4@"Ok"@H_502_4@)@H_502_4@@H_502_4@
- alert.show@H_502_4@()@H_502_4@@H_502_4@
- }@H_502_4@
2@H_502_4@.APPDelegate.swift调用
- //@H_502_4@@H_502_4@@H_502_4@
- //AppDelegate.swift@H_502_4@@H_502_4@@H_502_4@
- //UITableViewDemo@H_502_4@@H_502_4@@H_502_4@
- //@H_502_4@@H_502_4@@H_502_4@
- //Createdby赵超on14-6-21.@H_502_4@@H_502_4@@H_502_4@
- //Copyright(c)2014年赵超.Allrightsreserved.@H_502_4@@H_502_4@@H_502_4@
- @H_502_4@
- importUIKit@H_502_4@
- @UIApplicationMain@H_502_4@@H_502_4@@H_502_4@
- AppDelegate@H_502_4@:UIResponder,UIApplicationDelegate{@H_502_4@@H_502_4@
- @H_502_4@
- window@H_502_4@:UIWindow?@H_502_4@@H_502_4@
- funcapplication(application:UIApplication,didFinishLaunchingWithOptionslaunchOptions@H_502_4@:NSDictionary?)->Bool{@H_502_4@@H_502_4@
- 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@
- //Overridepointforcustomizationafterapplicationlaunch.@H_502_4@@H_502_4@@H_502_4@
- varrootView=RootViewController()@H_502_4@
- varnav=UINavigationController(rootViewController:rootView)@H_502_4@
- self@H_502_4@.window@H_502_4@!@H_502_4@.rootViewController@H_502_4@=nav;@H_502_4@@H_502_4@
- 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@
- self@H_502_4@.window@H_502_4@!@H_502_4@.makeKeyAndVisible@H_502_4@()@H_502_4@@H_502_4@
- true@H_502_4@@H_502_4@@H_502_4@
- }@H_502_4@
- funcapplicationWillResignActive(application:UIApplication){@H_502_4@
- //Sentwhentheapplicationisabouttomovefromactivetoinactivestate.Thiscanoccurforcertaintypesoftemporaryinterruptions(suchasanincomingphonecallorSMSmessage)orwhentheuserquitstheapplicationanditbeginsthetransitiontothebackgroundstate.@H_502_4@@H_502_4@@H_502_4@
- //UsethismethodtopauSEOngoingtasks,disabletimers,andthrottledownOpenGLESframerates.Gamesshouldusethismethodtopausethegame.@H_502_4@@H_502_4@@H_502_4@
- }@H_502_4@
- funcapplicationDidEnterBackground(application:UIApplication){@H_502_4@
- //Usethismethodtoreleasesharedresources,saveuserdata,invalidatetimers,andstoreenoughapplicationstateinformationtorestoreyourapplicationtoitscurrentstateincaseitisterminatedlater.@H_502_4@@H_502_4@@H_502_4@
- //Ifyourapplicationsupportsbackgroundexecution,thismethodiscalledinsteadofapplicationWillTerminate:whentheuserquits.@H_502_4@@H_502_4@@H_502_4@
- funcapplicationWillEnterForeground(application:UIApplication){@H_502_4@
- //Calledaspartofthetransitionfromthebackgroundtotheinactivestate;hereyoucanundomanyofthechangesmadeonenteringthebackground.@H_502_4@@H_502_4@@H_502_4@
- funcapplicationDidBecomeActive(application:UIApplication){@H_502_4@
- //Restartanytasksthatwerepaused(ornotyetstarted)whiletheapplicationwasinactive.IftheapplicationwasprevIoUslyinthebackground,optionallyrefreshtheuserinterface.@H_502_4@@H_502_4@@H_502_4@
- funcapplicationWillTerminate(application:UIApplication){@H_502_4@
- //Calledwhentheapplicationisabouttoterminate.Savedataifappropriate.SeealsoapplicationDidEnterBackground:.@H_502_4@@H_502_4@@H_502_4@
- }@H_502_4@
3.效果
完整工程代码 :https://github.com/whzhaochao/IOS-Swift-UITableViewDemo