1 import UIKit 2 3 class ViewController: UIViewController ,UITableViewDelegate, UITableViewDataSource 4 { 5 var tableView : UITableView? 6 var items :NSMutableArray? 7 var leftBtn:UIButton? 8 9 override func viewDidLoad() { 10 super.viewDidLoad() 11 self.title = "I love Swift" 12 self.items = NSMutableArray() 13 // self.items?.addObject("1","2") 14 // Do any additional setup after loading the view, typically from a nib. 15 setupViews() 16 setupRightBarButtonItem() 17 setupLeftBarButtonItem(); 18 } 19 20 func setupViews() 21 { 22 self.tableView = UITableView(frame:self.view!.frame) 23 self.tableView!.delegate = self 24 self.tableView!.dataSource = self 25 self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") 26 self.view?.addSubview(self.tableView) 27 } 28 29 func setupLeftBarButtonItem() 30 { 31 self.leftBtn = UIButton.buttonWithType(UIButtonType.Custom) as? UIButton 32 self.leftBtn!.frame = CGRectMake(0,0,50,40) 33 self.leftBtn?.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal) 34 self.leftBtn?.setTitle("Edit", forState: UIControlState.Normal) 35 self.leftBtn!.tag = 100 36 self.leftBtn!.userInteractionEnabled = false 37 self.leftBtn?.addTarget(self, action: "leftBarButtonItemClicked", forControlEvents: UIControlEvents.TouchUpInside) 38 var barButtonItem = UIBarButtonItem(customView: self.leftBtn) 39 self.navigationItem!.leftBarButtonItem = barButtonItem 40 } 41 42 func setupRightBarButtonItem() 43 { 44 var barButtonItem = UIBarButtonItem(title: "Add", style: UIBarButtonItemStyle.Plain, target: self, action: "rightBarButtonItemClicked") 45 self.navigationItem!.rightBarButtonItem = barButtonItem 46 } 47 48 func rightBarButtonItemClicked() 49 { 50 51 var row = self.items!.count 52 var indexPath = NSIndexPath(forRow:row,inSection:0) 53 self.items?.addObject("1") 54 self.tableView?.insertRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Left) 55 self.leftBtn!.userInteractionEnabled = true 56 } 57 58 func leftBarButtonItemClicked() 59 { 60 if (self.leftBtn!.tag == 100) 61 { 62 self.tableView?.setEditing(true, animated: true) 63 self.leftBtn!.tag = 200 64 self.leftBtn?.setTitle("Done", forState: UIControlState.Normal) 65 } 66 else 67 { 68 self.tableView?.setEditing(false, animated: true) 69 self.leftBtn!.tag = 100 70 self.leftBtn?.setTitle("Edit", forState: UIControlState.Normal) 71 } 72 73 } 74 75 76 override func didReceiveMemoryWarning() { 77 super.didReceiveMemoryWarning() 78 // Dispose of any resources that can be recreated. 79 } 80 81 func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int 82 { 83 return self.items!.count 84 } 85 86 func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! 87 { 88 let cell = tableView .dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell 89 cell.textLabel.text = String(format: "%i", indexPath.row+1) 90 return cell 91 } 92 93 func tableView(tableView: UITableView!, canEditRowAtIndexPath indexPath: NSIndexPath!) -> Bool 94 { 95 return true 96 } 97 98 func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) 99 { 100 self.items?.removeObjectAtIndex(indexPath.row) 101 102 self.tableView?.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Top) 103 if (self.items!.count == 0) 104 { 105 self.leftBtn!.userInteractionEnabled = false 106 } 107 108 } 109 110 func tableView(tableView: UITableView!, editingStyleForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCellEditingStyle 111 { 112 return (UITableViewCellEditingStyle.Delete) 113 } 114 115 func tableView(tableView: UITableView!, canMoveRowAtIndexPath indexPath: NSIndexPath!) -> Bool 116 { 117 return true 118 } 119 120 func tableView(tableView: UITableView!, moveRowAtIndexPath sourceIndexPath: NSIndexPath!, toIndexPath destinationIndexPath: NSIndexPath!) 121 { 122 self.tableView?.moveRowAtIndexPath(sourceIndexPath, toIndexPath: destinationIndexPath) 123 self.items?.exchangeObjectAtIndex(sourceIndexPath.row, withObjectAtIndex: destinationIndexPath.row) 124 } 125 126 func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) 127 { 128 println("row = %d",indexPath.row) 129 } 130 131 132 133 134 135 136 137 }
分享一个官网文档的链接,有兴趣的童鞋可以去看看,有关OC 和 Swift 的兼容问题
https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_75