• swift 笔记


    跳转

        let tableViewController = TableViewController();
        tableViewController.test = "参数"
        self.navigationController!.pushViewController(tableViewController, animated:true)
    

    模态跳转

        let tableViewController = TableViewController()
        self.present(tableViewController, animated: true, completion: nil)
    

    跳转后黑屏

        // 设置背景色
        self.view.backgroundColor = UIColor.white
    

    autolayout

        // 创建button-button3并设置样式
        // ...
        // 设置 translates... 为 false,否则无法用 autolayout
        button.translatesAutoresizingMaskIntoConstraints = false
        button2.translatesAutoresizingMaskIntoConstraints = false
        button3.translatesAutoresizingMaskIntoConstraints = false
        // 创建一个控件数组
        let views:[String:AnyObject] = ["button": button,"button2": button2,"button3": button3]
        // 创建水平方向约束
        self.view.addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "H:|-5-[button(100)]-5-[button2(100)]",
            options:.alignAllTop,
            metrics: nil,
            views: views
        ))
        // 创建垂直方向约束
        self.view.addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "V:|-100-[button(50)]-20-[button3(40)]",
            options:.alignAllLeft,
            metrics: nil,
            views: views
        ))
        // 设置button3宽度
        let constraint:NSLayoutConstraint = NSLayoutConstraint(
            item: button3,
            attribute: .width,
            relatedBy: .equal,
            toItem: nil,
            attribute: .notAnAttribute,
            multiplier: 1.0,
            constant: 140
        )
        // 添加约束
        self.view.addConstraint(constraint)
    

    autolayout 获取不到 size

        // autolayout布局时,控件上下左右需要触碰到边缘,否则无法获取 size
        // 如:
        // H:|[box(40)]|
        // V:|-80-[box(999)]-80-|
        // 获取 size 要在 viewDidLayoutSubviews 方法内
        override func viewDidLayoutSubviews() {
            scrollView.contentSize = box.frame.size
        }
    

    tableView 设置数据

        // 在协议中设置数据源(UITableViewDataSource),代理(UITableViewDelegate)
        class TableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
            var table:UITableView!
            let arr:[String] = ["我是谁", "我从哪里来", "要到哪里去"]
            func setTableView() {
                let rect = self.view.frame
                table = UITableView(frame: rect)
                self.view.addSubview(table)
                //设置数据源
                self.table.dataSource = self
                //设置代理
                self.table.delegate = self
                //注册UITableView,cellID为重复使用cell的Identifier
                self.table.register(UITableViewCell.self, forCellReuseIdentifier: "cellID")
            }
            func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                return arr.count
            }
            //设置section的数量
            func numberOfSections(in tableView: UITableView) -> Int {
                return 1
            }
            //设置tableview的cell
            func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                let cell = (table.dequeueReusableCell(withIdentifier: "cellID", for: indexPath)) as UITableViewCell
                // cell.textLabel?.text = arr[indexPath.row]
                
                
                let button = UIButton()
                button.setTitle("测试(indexPath.row)", for: .normal)
                button.backgroundColor = UIColor.black
                button.addTarget(self, action: #selector(clickButton(_:)), for: .touchUpInside)
                button.tag = indexPath.row
                cell.addSubview(button)
                let views:[String:AnyObject] = ["button": button]
                button.translatesAutoresizingMaskIntoConstraints = false
                //创建水平方向约束
                cell.addConstraints(NSLayoutConstraint.constraints(
                    withVisualFormat: "H:|-5-[button]-5-|",
                    options:.alignAllTop,
                    metrics: nil,
                    views: views
                ))
                cell.addConstraints(NSLayoutConstraint.constraints(
                    withVisualFormat: "V:|-10-[button]",
                    options:.alignAllLeft,
                    metrics: nil,
                    views: views
                ))
                return cell
            }
            // 点击事件
            @objc func clickButton (_ test: UIButton) {
                print(arr[test.tag])
            }
            
            override func viewDidLoad() {
                super.viewDidLoad()
                self.setTableView()
            }
        }
    
  • 相关阅读:
    MVC5中页面传值,保存,回传
    MVC ajax传递model,ajax接收model
    创建对象的常用四种模式和优缺点
    Promise的用法
    关于深拷贝与浅拷贝
    作用域和作用域链
    谈谈JS中的闭包
    encodeURI和encodeURIComponent区别
    window.scrollTo()
    浮动和常用清除浮动的四种方法
  • 原文地址:https://www.cnblogs.com/NKnife/p/9234054.html
Copyright © 2020-2023  润新知