【swift_3】swift之UITableView和UINavigation视图控制器

这篇博客详细介绍了在Swift中如何使用UITableView与UINavigationController。通过实例代码,讲解了在AppDelegate、ViewController、SecondViewController及ThirdViewController中的实现过程,包括UITableViewDataSource和UITableViewDelegate的设置,以及导航按钮的点击事件处理。读者可以通过提供的Demo链接进行实践。
摘要由CSDN通过智能技术生成

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

//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]
        //在这里找不到数组exchange的方法, 所以采用先移除后添加来实现交换位置
        _dataSource.removeAtIndex(sourceIndexPath.row)
        _dataSource.insert(itemToMove, atIndex: destinationIndexPath.row)
    }

    //cell点击事件
    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)
    }









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值