【问题标题】:Understanding Instantiation in Swift - Why not require in custom class理解 Swift 中的实例化 - 为什么在自定义类中不需要
【发布时间】:2015-11-11 12:39:22
【问题描述】:

据我了解,在 Swift 中您不需要导入类(仅适用于某些框架),但需要导入,但令我惊讶的是,这可能不是真的,我创建了一个自定义类 (CustomCell.swift)对于我在 UITableView 中使用的自定义单元格,显然在使用它之前不需要创建自定义类的实例。这是我的使用方法。

自定义单元类

//  CustomCell.swift

import UIKit

class CustomCell: UITableViewCell {

    @IBOutlet weak var labelDisplayWattage: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

视图控制器

//  ViewController.swift
import UIKit

class ViewController: UIViewController, UITableViewDataSource {

// some code ...

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("reusableCell", forIndexPath: indexPath) as! CustomCell

        cell.labelDisplayWattage.text = String(totalWattageList[indexPath.row])

        return cell
    }

}

如您所见,没有 CustomCell 类的实例可用于调用方法 cellForRowAtIndexPath (as! CustomCell)。

谁能告诉我为什么在方法cellForRowAtIndexPath 中使用它之前不需要创建类CustomCell 的实例?我期待看到类似...

var myCustomCell = CustomCell()

谢谢

【问题讨论】:

    标签: ios swift uitableview swift2


    【解决方案1】:

    根据数据源方法的苹果文档

     func dequeueReusableCellWithIdentifier(_ identifier: String,forIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    

    出于性能原因,表视图的数据源通常应该 将单元格分配给其行时重用 UITableViewCell 对象 tableView:cellForRowAtIndexPath: 方法。表视图维护一个 数据源具有的 UITableViewCell 对象的队列或列表 标记为可重复使用。何时从您的数据源对象调用此方法 要求为表格视图提供一个新单元格。 此方法出队 如果一个现有单元可用,则创建一个新单元 您之前注册的类或 nib 文件,并将其添加到 表。

    所以基本上如果队列为空,它会为您创建一个新对象。

    来源:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/#//apple_ref/occ/instm/UITableView/dequeueReusableCellWithIdentifier:forIndexPath

    【讨论】:

      【解决方案2】:

      您现在看到的代码下面有一个非常重要的概念。如果您使用情节提要,表格视图和集合视图可以选择使用原型单元格。原型单元格是可以/将与表格视图或集合视图一起使用的单元格模板。如果您的表格/集合视图设置为使用原型单元格,则无需创建新单元格,UIKit 会在您通过调用 tableView.dequeueReusableCellWithIdentifier 请求队列单元格时为您创建一个。

      使用原型单元时,请考虑以下几点:

      //  ViewController.swift
      import UIKit
      
      class ViewController: UIViewController, UITableViewDataSource {
      
      // some code ...
      
          func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
              var cell = tableView.dequeueReusableCellWithIdentifier("reusableCell", forIndexPath: indexPath) as? CustomCell
              if nil == cell {
                  cell = CustomCell()
                  // more cell options can be set here
              }
              cell.labelDisplayWattage.text = String(totalWattageList[indexPath.row])
              return cell
          }
      
      }
      

      【讨论】:

      • 是的,我使用的是原型单元。非常感谢
      【解决方案3】:
      let cell = tableView.dequeueReusableCellWithIdentifier("reusableCell", forIndexPath: indexPath) as! CustomCell
      

      CustomCell的实例化是tableview的职责,我们只取cell,如果取的时候cell没有实例化,tableview会先实例化,然后返回给我们。

      这种行为是架构设计的,它不是 swift 特有的。它也适用于objective-c。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多