How to reference UITableViewController from a UITableViewCell class?

前端 未结 6 450
余生分开走
余生分开走 2020-12-08 06:33

I have the following:

  1. UITableViewController
  2. UITableView
  3. Custom UITableViewCell subclass

I used a .xib file for the cell, which

6条回答
  •  感情败类
    2020-12-08 07:34

    I don't think you should do that and probably you're doing something wrong but if you really-really need it then just have a property in your CustomCell class.

    weak var viewController : UIViewController
    

    then when you create the cell in cellForRowAtIndexPath set the property

    cell.viewController = self
    

    after that you can easily access the view controller from within the cell code:

    self.viewController.doSomething()
    

    But again, in my opinion, you should redesign your code. The cell should not care about the controller. It should care only about displaying itself and nothing else

提交回复
热议问题