Swift- lazy 懒加载

我们在使用OC 的时候会写一些懒加载 用的的时候才创建 提高了系统的性能 
Swift 给我们提供了一个属性 lazy 专门的延迟加载属性 方便了我们的编程 也提高了系统的性能 
我们在写UI控件要多使用懒加载方式 
import UIKit
import Foundation


let cellName: String = "myCell"


class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
    
    var clickCount: Int = 0
    
    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        let myButton = UIButton()
        
        myButton .setTitle("腾讯", forState: UIControlState.Normal)
        
        
        myButton.backgroundColor = UIColor.cyanColor()
        
        myButton.frame = CGRect(x: 0, y: 160, width: 375, height: 50)
        
        myButton .addTarget(self, action: "btnClick:" , forControlEvents: UIControlEvents.TouchUpInside)
        
        self.view.addSubview(myButton)
        
        self.view.addSubview(myLabel)
        
        // Do any additional setup after loading the view, typically from a nib.
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20;
    }
    
    func btnClick (button :UIButton) {
        NSLog("点击")
    }
    
    
    lazy var myLabel: UILabel = {
        //color 是常量
        let color = UIColor.redColor()
        
        self.view.backgroundColor = color
        
        let rect = CGRect(x: 0, y: 100, width: 375, height: 50)
        
        let myLabel = UILabel()
        
        //mark -
        
        myLabel.frame = rect
        
        myLabel.text = "百度"
        
        myLabel.backgroundColor = UIColor.greenColor()
        
        myLabel.textAlignment = NSTextAlignment.Center
        
        self.view.addSubview(myLabel)
        
        return myLabel
    }()
    
    //mark - 懒加载
    /*
    lazy var buyButton: UIButton = {
        
        let buyButton = UIButton(type: UIButtonType.Custom)
        buyButton.setTitle("付款", forState: UIControlState.Normal)
        buyButton.setBackgroundImage(UIImage(named: "button_cart_add"), forState: UIControlState.Normal)
        buyButton.layer.cornerRadius = 15
        buyButton.layer.masksToBounds = true
        return buyButton
    }()
    */
    
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCellWithIdentifier(cellName);UITableViewCell?()
        if (cell == nil) {
            
            cell = UITableViewCell(style: .Default, reuseIdentifier: cellName)
        }
        cell!.textLabel?.text = "test"
        return cell!
    }
    
    
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值