将方法委托给UItableViewCell Swift

问题描述:

我有一个带有单元格的UItableView形式的社交网络供稿。现在,每个单元格都有一个图像,在触发偶数时会发出动画。现在,此事件为字符串形式,将在每个单元格处触发。事件的选项在另一个类(NSObject类型)中定义。

I have a Social Network Feed in form UItableView which has a cell. Now each cell has an image that animates when an even is triggered. Now, This event is in form of a string, will be triggered at every cell. the options for the event are defined in another class(of type NSObject).

我的问题:
我在表视图中构造了一个协议委托方法,只要为每个单元格触发事件,就会调用该方法。然后,我在UITableViewCell类中定义此函数,因为我的图像将以此为动画。
一切运行良好,但是我无法弄清楚如何将TableView类的委托分配给单元格类。我的意思是,如何在cellView类中使用 UITableView.delegate = self 。我尝试使用静态变量,但是它不起作用。

My issue: I constructed a protocol delegate method in table view, which will be called whenever the event is triggered for each cell. Then, I define this function in UITableViewCell Class, since my the image will be animating on that. All is working well but I am unable to figure out how to assign the delegate of TableView class to cell class. What I mean is, how can I use UITableView.delegate = self in cellView class. I have tried using a static variable, but it doesn't work.

我已经研究协议已有一段时间了,但确实无法找到解决方案。

I have been playing around the protocols for a while now but really unable to figure out a solution to this.

我希望我清楚。如果没有,我将在评论中提供一个示例。抱歉,这是一个机密项目,我无法透露所有细节。

I hope I am clear. If not, I will provide with an example in the comments. I am sorry, This is a confidential project and I cant reveal all details.

如果我正确理解了您的问题,也许可以帮忙:

If I correctly understood your question, maybe this could help:

class ViewController: UIViewController, YourCustomTableDelegate {

@IBOutlet weak var tableView: YourCustomTableView!  

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.customTableDelegate = self
    }


    // table delegate method
    func shouldAnimateCell(at indexPath: IndexPath) {
       if let cell = tableView.cellForRow(at: indexPath) {
           cell.animate(...)
       }    
    }
}