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

前端之家收集整理的这篇文章主要介绍了【swift_3】swift之UITableView和UINavigation视图控制器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Demo:链接: http://pan.baidu.com/s/1jGBwWYU 密码: jn5b

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会报错,完成之后就好了

@H_403_20@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",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

@H_403_20@override func viewDidLoad() { super.viewDidLoad() self.navigationItem.title = "Third" self.view.backgroundColor = UIColor.greenColor() var button = UIButton(frame: self.view.bounds) button.addTarget(self,forControlEvents: UIControlEvents.TouchUpInside) button.setTitle("点击屏幕去根视图",forState: UIControlState.Normal) self.view.addSubview(button) } func buttonClick() { print("buttonClick") self.navigationController?.popToRootViewControllerAnimated(true) } 原文链接:https://www.f2er.com/swift/326882.html

猜你在找的Swift相关文章