想在 Swift 中重新定义 willDisplayCell 方法.如何重新定义类方法 [英] Want to redefine willDisplayCell method in Swift. How to redefine class methods

查看:132
本文介绍了想在 Swift 中重新定义 willDisplayCell 方法.如何重新定义类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的自定义单元类中使用了某些 UI 组件,并且我想在 willDisplayCell 方法中配置这些组件.原来的定义是

I am using certain UI components within my custom cell class and I want to configure those in the willDisplayCell method. The original definition is

public func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)

现在使用 cell.myTextField.text = "customise"给出错误,单元格没有任何组件作为 myTextField

Now using cell.myTextField.text = "customise" gives out error that cell does not have any component as myTextField

要解决这个问题,我将不得不重新定义方法

to resolve this I will have to redefine the method to

public func tableView(tableView: UITableView, willDisplayCell cell: **myCustomCellClass**, forRowAtIndexPath indexPath: NSIndexPath)

任何有关如何执行此操作以及随后如何在 swift 中重新定义任何现有类方法的线索都会很棒.提前致谢.

Any leads on how to do this and subsequently how to redefine any present class methods in swift will be great. Thanks in advance.

推荐答案

很遗憾,您无法更改参数类型,因为这会更改界面.您可以将方法的返回类型更改为子类型,但不能将方法参数更改为子类型.

Unfortunately, you cannot change the parameter type because that would change the interface. You could get away with changing the return type of a method to a subtype but you cannot change method parameter to a subtype.

你真的有一个选择,一个动态的演员:

You have one option really, a dynamic cast:

public func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
   // use as? or as! to cast UITableViewCell to your desired type
}

当然,你可以通过多种方式来做同样的事情,例如,你可以创建一个重定向方法:

Of course, you can do the same in multiple ways, for example, you can create a redirection method:

public func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
   self.tableView(tableView, willDisplayMyCell: cell as! MyCellClass, forRowAtIndexPath: indexPath)
}

private func tableView(tableView: UITableView, willDisplayMyCell myCell: MyCellClass, forRowAtIndexPath indexPath: NSIndexPath) {
   // do something
}

并且您可以将其抽象为 UITableViewDelegate 的子协议:

and you can abstract that into a subprotocol of UITableViewDelegate:

class MyTableViewCell : UITableViewCell {

}

protocol MyTableViewDelegate : UITableViewDelegate {
    associatedtype CellClass: UITableViewCell

    func tableView(
        tableView: UITableView,
        willDisplayMyCell cell: CellClass,
        forRowAtIndexPath indexPath: NSIndexPath)
    }

extension MyTableViewDelegate {
    func tableView(
        tableView: UITableView,
        willDisplayCell cell: UITableViewCell,
        forRowAtIndexPath indexPath: NSIndexPath) {

        self.tableView(tableView, willDisplayMyCell: cell as! CellClass, forRowAtIndexPath: indexPath)
    }    
}

很酷的一点是协议是可重用的,然而,这一切都归结为 as?as! 动态转换.

The cool thing about this is that the protocol is reusable, however, it all boils down to as? or as! dynamic cast.

这篇关于想在 Swift 中重新定义 willDisplayCell 方法.如何重新定义类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆