Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Swift- lazy 懒加载大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我们在使用OC 的时候会写一些懒加载 用的的时候才创建 提高了系统的性能 Swift 给我们提供了一个属性 lazy 专门的延迟加载属性 方便了我们的编程 也提高了系统的性能 我们在写UI控件要多使用懒加载方式 import UIKit import Foundation let cellName: String = "myCell" class ViewController: UIVie
我们在使用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,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.
    }


}

大佬总结

以上是大佬教程为你收集整理的Swift- lazy 懒加载全部内容,希望文章能够帮你解决Swift- lazy 懒加载所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。