加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 02:05:53 所属栏目:百科 来源:网络整理
导读: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

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

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

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)
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读