设置变量 "as" a Class
Setting a Variable "as" a Class
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCellWithIdentifier("HeaderCell") as! CustomHeaderCell
你可以看到上面的代码。我只是想问一下我们如何以及为什么可以或应该将 tableViewCell 设置为 Class?在这种情况下,它是 CustomHeaderCell。有关代码的更多信息,请看这里:我只是按照我在网上看到的指南创建自定义 header。
当 (a) 您知道被 return 编辑的对象的实际 class,而编译器无法合理地知道这一点时,您应该使用此模式; (b) 然后你需要使用这个 subclass 的 methods/properties。
在这种情况下,您的情节提要中可能有一个单元格原型,它指定了一个 CustomHeaderCell
基础 class(或者您已经注册了一个 class 或 NIB,这样您就知道HeaderCell
标识符将 return 一个 CustomHeaderCell
实例)。此外,您可能想引用此 CustomHeaderCell
的属性(例如,设置其自定义 UILabel
网点或其他内容的 text
属性)。
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCellWithIdentifier("HeaderCell") as! CustomHeaderCell
你可以看到上面的代码。我只是想问一下我们如何以及为什么可以或应该将 tableViewCell 设置为 Class?在这种情况下,它是 CustomHeaderCell。有关代码的更多信息,请看这里:我只是按照我在网上看到的指南创建自定义 header。
当 (a) 您知道被 return 编辑的对象的实际 class,而编译器无法合理地知道这一点时,您应该使用此模式; (b) 然后你需要使用这个 subclass 的 methods/properties。
在这种情况下,您的情节提要中可能有一个单元格原型,它指定了一个 CustomHeaderCell
基础 class(或者您已经注册了一个 class 或 NIB,这样您就知道HeaderCell
标识符将 return 一个 CustomHeaderCell
实例)。此外,您可能想引用此 CustomHeaderCell
的属性(例如,设置其自定义 UILabel
网点或其他内容的 text
属性)。