【发布时间】: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