Demo:链接: http://download.csdn.net/download/riven_wn/9401961
AppDelegate
- 
var window: UIWindow?  
- 
  
- 
  
- 
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool  
- 
    {  
- 
        var VC = ViewController()  
- 
        var nav = UINavigationController(rootViewController: VC)  
- 
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)  
- 
        self.window!.rootViewController = nav  
- 
        self.window?.backgroundColor = UIColor.whiteColor()  
- 
        self.window?.makeKeyAndVisible()  
- 
     
- 
        return true  
- 
    }  
 
ViewController
引入代理,注意这里在完成dataSource的几个方法之前,UITableViewDataSource会报错,完成之后就好了
- 
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource  
 
定义tableView 和 数据源
- 
var _tableView:UITableView?  
- 
var _dataSource = ["星期一","星期二","星期三","星期四","星期五","星期六","星期日"]  
- 
  
- 
override func viewDidLoad()  
- 
{  
- 
    super.viewDidLoad()  
- 
      
- 
      
- 
    self.navigationItem.title = "单元格滑动可删除"  
- 
    var leftBtn = UIBarButtonItem(title: "Add", style: UIBarButtonItemStyle.Plain, target: self, action:"add")  
- 
    var rightBtn = UIBarButtonItem(title: "Move", style: UIBarButtonItemStyle.Plain, target: self, action: "edit:")  
- 
    rightBtn.tag = 100  
- 
    self.navigationItem.leftBarButtonItem = leftBtn  
- 
    self.navigationItem.rightBarButtonItem = rightBtn  
- 
      
- 
      
- 
    _tableView = UITableView(frame: self.view!.bounds)  
- 
    _tableView?.delegate = self  
- 
    _tableView?.dataSource = self  
- 
    self.view .addSubview(_tableView!)  
- 
}  
 
导航按钮点击事件
- 
  
- 
    func add()  
- 
    {  
- 
        println("add sth")  
- 
        _dataSource .append("一周就七天")  
- 
        _tableView?.reloadData()  
- 
    }  
- 
      
- 
    func edit(rightBtn:UIBarButtonItem)  
- 
    {  
- 
        println("edit tableView")  
- 
        if(rightBtn.tag == 100)  
- 
        {  
- 
            _tableView?.setEditing(true, animated: true)  
- 
            rightBtn.tag = 200  
- 
            rightBtn.title = "done"  
- 
        }  
- 
        else  
- 
        {  
- 
            _tableView?.setEditing(false, animated: true)  
- 
            rightBtn.tag = 100  
- 
            rightBtn.title = "edit"  
- 
        }  
- 
    }  
 
UITableViewDataSource UITableViewDelegate
- 
  
- 
      
- 
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int  
- 
    {  
- 
        return _dataSource.count  
- 
      
- 
    }  
- 
      
- 
      
- 
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell  
- 
    {  
- 
        let  identifier = "Cell"  
- 
        var cell = tableView.dequeueReusableCellWithIdentifier(identifier)as? UITableViewCell;  
- 
        if(cell == nil)  
- 
        {  
- 
            cell = UITableViewCell(style:UITableViewCellStyle.Default, reuseIdentifier: identifier)  
- 
        }  
- 
        cell?.textLabel?.text = _dataSource[indexPath.row]  
- 
        return cell!  
- 
    }  
- 
      
- 
      
- 
    func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool  
- 
    {  
- 
        return true  
- 
    }  
- 
      
- 
      
- 
    func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle  
- 
    {  
- 
          
- 
        if(_tableView?.editing == false)  
- 
        {  
- 
            return UITableViewCellEditingStyle.Delete  
- 
        }  
- 
          
- 
        else  
- 
        {  
- 
            return UITableViewCellEditingStyle.None  
- 
        }  
- 
    }  
- 
      
- 
      
- 
    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)  
- 
    {  
- 
        _dataSource.removeAtIndex(indexPath.row)  
- 
        _tableView?.reloadData()  
- 
    }  
- 
      
- 
      
- 
    func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool  
- 
    {  
- 
        return true  
- 
    }  
- 
      
- 
      
- 
    func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath)  
- 
    {  
- 
        _tableView?.moveRowAtIndexPath(sourceIndexPath, toIndexPath: destinationIndexPath)  
- 
        var itemToMove = _dataSource[sourceIndexPath.row]  
- 
          
- 
        _dataSource.removeAtIndex(sourceIndexPath.row)  
- 
        _dataSource.insert(itemToMove, atIndex: destinationIndexPath.row)  
- 
    }  
- 
  
- 
      
- 
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)  
- 
    {  
- 
        switch indexPath.row  
- 
        {  
- 
        case 0:  
- 
            self.navigationController?.pushViewController(FirstViewController(), animated: true)  
- 
  
- 
        case 1:  
- 
            self .presentViewController(SecondViewController(), animated: true, completion: nil)  
- 
              
- 
        case 2:  
- 
            self.navigationController?.pushViewController(ThirdViewController(), animated: true)  
- 
        default:  
- 
            var alert = UIAlertView(title: "提示", message: "不是前三", delegate: nil, cancelButtonTitle: "cancel")  
- 
            alert.show()  
- 
        }  
- 
    }  
 
SecondViewController 
- 
override func viewDidLoad() {  
- 
        super.viewDidLoad()  
- 
  
- 
        self.navigationItem.title = "Second"  
- 
        self.view.backgroundColor = UIColor.redColor()  
- 
          
- 
        var button = UIButton(frame: self.view.bounds)  
- 
        button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)  
- 
        button.setTitle("点击屏幕返回", forState: UIControlState.Normal)  
- 
        self.view.addSubview(button)  
- 
    }  
- 
  
- 
    func buttonClick()  
- 
    {  
- 
        print("buttonClick")  
- 
        self .dismissViewControllerAnimated(true, completion: nil)  
- 
    }  
 
ThirdViewController
- 
override func viewDidLoad() {  
- 
        super.viewDidLoad()  
- 
  
- 
        self.navigationItem.title = "Third"  
- 
        self.view.backgroundColor = UIColor.greenColor()  
- 
          
- 
        var button = UIButton(frame: self.view.bounds)  
- 
        button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)  
- 
        button.setTitle("点击屏幕去根视图", forState: UIControlState.Normal)  
- 
        self.view.addSubview(button)  
- 
    }  
- 
      
- 
    func buttonClick()  
- 
    {  
- 
        print("buttonClick")  
- 
        self.navigationController?.popToRootViewControllerAnimated(true)  
- 
    }  
 【swift_3】swift之UITableView和UINavigation视图控制器
原文:http://blog.csdn.net/jiaxin_1105/article/details/50999632