Swift3.0起航吧

作者: Easy_VO | 来源:发表于2016-09-27 21:22 被阅读254次

    swift3.0已经到来,2.3的项目一运行,崩溃个百八个报错是相当正常的。

    还是来进入Swift3.0的大坑中吧,不一定要用在实际的项目中去,但是不学习是不好的,来学起吧-。-

    3.0变化了很多的地方,最基本的创建UI控件

    UIImageView

        //imageview
        let iphoto = UIImageView()
        iphoto.frame = CGRect(x:0,y:0,width:self.view.frame.size.width,height:200)
        iphoto.image = UIImage(named:"zdzz")
        self.view.addSubview(iphoto)
    

    UILabel

    //label
        let iLabel = UILabel()
    //  iLebel.adjustsFontSizeToFitWidth = true
        iLabel.font = UIFont.systemFont(ofSize: 14)
        iLabel.textColor = UIColor.white
        iLabel.frame = CGRect(x:UIScreen.main.bounds.size.width/2 - 90,y:20,width:180,height:20)
        iLabel.text = "这是从swift3.0开始的label"
    //  iLabel.backgroundColor = UIColor.lightGray
        iphoto.addSubview(iLabel)
    

    UIButton

     //swift3.0的button
        let button = UIButton()
        button.frame = CGRect(x:0, y:(screenSize.height-50),width:screenSize.width,height:50)
        button.backgroundColor = UIColor.blue
        button.setImage((UIImage (named: "zixun")), for: .normal)
        button.setTitle("佐罗", for: .normal)
        button.layer.cornerRadius = 15
        button.clipsToBounds = true
        button.addTarget(self, action: #selector(buttontapped), for: .touchUpInside)
        self.view.addSubview(button)
    

    手写tableview

    import UIKit
    
    class ViewController: UIViewController ,UITableViewDataSource,UITableViewDelegate{
    
    var tableView:UITableView! = nil
    let screenSize = UIScreen.main.bounds.size
    override func viewDidLoad() {
        super.viewDidLoad()
    tableView = UITableView()
        tableView.backgroundColor = UIColor.red
        tableView.delegate = self
        tableView.dataSource = self
        tableView.frame = CGRect(x:0,y:200,width:UIScreen.main.bounds.size.width,height:UIScreen.main.bounds.size.height - 200 - 50)
        self.view.addSubview(tableView)
        }
    
    uitableview的代理方法
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return 20;
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        //设置单元格数据
    //        var cell = tableView.dequeueReusableCell(withIdentifier: "CELL")
       
    //        if cell == nil {
    //            cell = UITableViewCell(style: .default, reuseIdentifier: "CELL")
    //        }
    //        
    //        cell?.textLabel?.text = "let's fucking"
        
        //cell标志符,使cell能够重用(如果不需要重用,是不是可以有更简单的配置方法?)
        let indentifier:String = "cell"
        //注册自定义cell到tableview中,并设置cell标识符为indentifier(nibName对应UItableviewcell xib的名字)
        let nib:UINib = UINib(nibName:"TableViewCell", bundle: nil)
        tableView.register(nib, forCellReuseIdentifier: indentifier)
        //从tableview中获取标识符为papercell的cell
        let cell:TableViewCell = tableView.dequeueReusableCell(withIdentifier: indentifier) as! TableViewCell
        
        return cell
    }
    
    func buttontapped(sender: UIButton) {
        print("swift3.0Go~")
    }
    

    自定义Cell

    上面的代码中,cellForRowAt中没有注释的部分为自定义cell的代码

    import UIKit
    
    class TableViewCell: UITableViewCell {
    
    @IBOutlet weak var cellImage: UIImageView!
    
    @IBOutlet weak var cellLabel: UILabel!
    override func awakeFromNib() {
        
        super.awakeFromNib()
        cellImage.layer.cornerRadius = 20;
        cellImage.clipsToBounds = true
        cellLabel.textColor = UIColor.blue
        
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    
    }  
    }
    

    结语:语法上很多变化,但是跟OC是很相似的,没事可以学学,以备以后稳定了上手-。0

    Simulator Screen Shot 2016年9月27日 21.20.38.png

    相关文章

      网友评论

        本文标题:Swift3.0起航吧

        本文链接:https://www.haomeiwen.com/subject/wkchyttx.html