美文网首页iOS学习好东西小知识点
Swift复习系列:Swift静态方法的使用、通过懒加载方式创建

Swift复习系列:Swift静态方法的使用、通过懒加载方式创建

作者: ZYiDa | 来源:发表于2017-08-29 10:31 被阅读425次

由于之前项目用的都是Objective-C,所以swift好多东西都忘了,现在抽空再学习一下。

第一部分,创建静态方法并使用
class MainView: UIView
{
    //Super init 方法
    override init(frame: CGRect)
    {
        super.init(frame: frame)
        self.viewFrame = frame
    }

    //静态方法
    class  func viewWith(frame:CGRect)-> MainView!
    {
        return MainCollectionView.init(frame: frame)
    }
}

说明

1.我们可以通过MainView.init(frame: CGRect.init(x: 0, y: 0, width: Width, height: Height))创建MainView的对象并初始化.
2.也可以通过MainView.viewWith(frame: CGRect.init(x: 0, y: 0, width: Width, height: Height))方式创建MainView的对象并初始化.

第二部分,通过懒加载方式创建tableView对象

懒加载的使用,一般为:lazy var 变量名:类型 = {操作}()
eg.

     lazy var myString:String =
    {
        return "String lazy load......"
    }()

下面是tableView的创建,

    lazy var mainList:UITableView =
    {
        ()->UITableView in
        let tempList = UITableView.init(frame: self.viewFrame!, style: UITableViewStyle.grouped)
        tempList.delegate = self
        tempList.dataSource = self
        tempList.rowHeight = 120.0
        return tempList
    }()
第三部分,创建tableView,实现代理方法
public func numberOfSections(in tableView: UITableView) -> Int 
{
   return 1
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
   return 6
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{
  let identifier:String = "cellIdentifier"
  var cell = tableView.dequeueReusableCell(withIdentifier: identifier)
  if cell == nil
  {
     cell = UITableViewCell.init(style: UITableViewCellStyle.default, reuseIdentifier: identifier)
  }
  cell?.selectionStyle = UITableViewCellSelectionStyle.none
  cell?.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
  return cell!
}


public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat 
{
   return 0.0001
}
public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat 
{
   return 0.0001
}

我是一个swift菜鸟,还请各位多多指教。

相关文章

  • Swift复习系列:Swift静态方法的使用、通过懒加载方式创建

    由于之前项目用的都是Objective-C,所以swift好多东西都忘了,现在抽空再学习一下。 第一部分,创建静态...

  • 使用Swift创建Swift模块 - 静态链接库

    使用Swift创建Swift模块 - 静态链接库 使用Swift创建Swift模块 - 静态链接库

  • 懒加载和单例

    懒加载 声明属性 重写get方法 Swift 单例的创建方式 方式一:创建单例工厂方法(重写alloc完善) 声明...

  • swift 中 Lazy,deinit用法

    * swift中懒加载写法,懒加载在Swift中是一个闭包。 懒加载详细介绍看这里 * OC中的dealloc方法...

  • swift —— tips swift 中的懒加载实现

    objc objc 对象的懒加载 swift swift 的懒加载 方式 1lazy 保证数据在用到的时候才会被加...

  • swift懒加载不需要使用weak unowned

    swift懒加载的闭包内部使用self,不会导致循环引用,原因是因为swift的懒加载闭包是非逃逸闭包@noesc...

  • 15-Swift中的懒加载

    懒加载的介绍 swift中也有懒加载的方式(苹果的设计思想:希望所有的对象在使用时才真正加载到内存中) 和OC不同...

  • 17.懒加载使用

    懒加载的介绍 swift中也有懒加载的方式(苹果的设计思想:希望所有的对象在使用时才真正加载到内存中) 和OC不同...

  • 22 Swift 懒加载

    懒加载的介绍 swift中也有懒加载的方式(苹果的设计思想:希望所有的对象在使用时才真正加载到内存中) 和OC不同...

  • Swift懒加载

    懒加载的介绍 swift中也有懒加载的方式(苹果的设计思想:希望所有的对象在使用时才真正加载到内存中) 和OC不同...

网友评论

    本文标题:Swift复习系列:Swift静态方法的使用、通过懒加载方式创建

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