1.新建RootViewController类
-
-
-
-
-
-
-
-
-
import UIKit
-
-
class RootViewController: UIViewController,UITableViewDelegate, UITableViewDataSource {
-
-
var tableView : UITableView?
-
var items = ["武汉","上海","北京","深圳","广州","重庆","香港","台海","天津"]
-
var leftBtn:UIButton?
-
var rightButtonItem:UIBarButtonItem?
-
-
override func viewDidLoad() {
-
super.viewDidLoad()
-
initView()
-
setupRightBarButtonItem()
-
setupLeftBarButtonItem()
-
self.leftBtn!.userInteractionEnabled = true
-
-
-
}
-
-
func initView(){
-
-
self.tableView=UITableView(frame:self.view.frame,style:UITableViewStyle.Plain)
-
-
self.tableView!.dataSource=self
-
-
self.tableView!.delegate = self
-
-
self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
-
self.view.addSubview(self.tableView!)
-
-
-
}
-
-
func setupLeftBarButtonItem()
-
{
-
self.leftBtn = UIButton.buttonWithType(UIButtonType.Custom) as? UIButton
-
self.leftBtn!.frame = CGRectMake(0,0,50,40)
-
self.leftBtn?.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
-
self.leftBtn?.setTitle("Edit", forState: UIControlState.Normal)
-
self.leftBtn!.tag = 100
-
self.leftBtn!.userInteractionEnabled = false
-
self.leftBtn?.addTarget(self, action: "leftBarButtonItemClicked", forControlEvents: UIControlEvents.TouchUpInside)
-
var barButtonItem = UIBarButtonItem(customView: self.leftBtn)
-
self.navigationItem!.leftBarButtonItem = barButtonItem
-
}
-
-
func leftBarButtonItemClicked()
-
{
-
println("leftBarButton")
-
if (self.leftBtn!.tag == 100)
-
{
-
self.tableView?.setEditing(true, animated: true)
-
self.leftBtn!.tag = 200
-
self.leftBtn?.setTitle("Done", forState: UIControlState.Normal)
-
-
self.rightButtonItem!.enabled=false
-
}
-
else
-
{
-
-
self.rightButtonItem!.enabled=true
-
self.tableView?.setEditing(false, animated: true)
-
self.leftBtn!.tag = 100
-
self.leftBtn?.setTitle("Edit", forState: UIControlState.Normal)
-
}
-
-
}
-
-
-
func setupRightBarButtonItem()
-
{
-
self.rightButtonItem = UIBarButtonItem(title: "Add", style: UIBarButtonItemStyle.Plain, target: self,action: "rightBarButtonItemClicked")
-
self.navigationItem!.rightBarButtonItem = self.rightButtonItem
-
-
}
-
-
func rightBarButtonItemClicked()
-
{
-
-
var row = self.items.count
-
var indexPath = NSIndexPath(forRow:row,inSection:0)
-
self.items.append("杭州")
-
self.tableView?.insertRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Left)
-
-
-
}
-
-
-
override func didReceiveMemoryWarning() {
-
super.didReceiveMemoryWarning()
-
-
}
-
-
-
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{
-
return self.items.count
-
}
-
-
-
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{
-
-
let cell = tableView .dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
-
var row=indexPath.row as Int
-
cell.textLabel.text=self.items[row]
-
cell.imageView.image = UIImage(named:"green.png")
-
return cell;
-
-
}
-
-
-
func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!){
-
var index=indexPath.row as Int
-
self.items.removeAtIndex(index)
-
self.tableView?.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Top)
-
NSLog("删除\(indexPath.row)")
-
}
-
-
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!){
-
let alert = UIAlertView()
-
alert.title = "提示"
-
alert.message = "你选择的是\(self.items[indexPath.row])"
-
alert.addButtonWithTitle("Ok")
-
alert.show()
-
}
-
-
-
-
}
2.APPDelegate.swift调用
-
-
-
-
-
-
-
-
-
import UIKit
-
-
@UIApplicationMain
-
class AppDelegate: UIResponder, UIApplicationDelegate {
-
-
var window: UIWindow?
-
-
-
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
-
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
-
-
var rootView=RootViewController()
-
var nav=UINavigationController(rootViewController:rootView)
-
self.window!.rootViewController = nav;
-
-
self.window!.backgroundColor = UIColor.whiteColor()
-
self.window!.makeKeyAndVisible()
-
-
return true
-
}
-
-
func applicationWillResignActive(application: UIApplication) {
-
-
-
}
-
-
func applicationDidEnterBackground(application: UIApplication) {
-
-
-
}
-
-
func applicationWillEnterForeground(application: UIApplication) {
-
-
}
-
-
func applicationDidBecomeActive(application: UIApplication) {
-
-
}
-
-
func applicationWillTerminate(application: UIApplication) {
-
-
}
-
-
-
}
3.效果
完整工程代码 :https://github.com/whzhaochao/IOS-Swift-UITableViewDemo