How to reference UITableViewController from a UITableViewCell class?

前端 未结 6 457
余生分开走
余生分开走 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:20

    Read the other comments about really wanting to use an alternate approach before trying this, but using this extension will allow you to get to the dataSource and delegate both of which should be the UITableViewController

    extension UITableViewCell {
        var tableView:UITableView? {
            get {
                for var view = self.superview ; view != nil ; view = view!.superview {
                    if view! is UITableView {
                        return (view! as UITableView)
                    }
                }
                return nil
            }
        }
    
        var tableViewDataSource:UITableViewDataSource? {
            get {
                return self.tableView?.dataSource
            }
        }
    
        var tableViewDelegate:UITableViewDelegate? {
            get {
                return self.tableView?.delegate
            }
        }
    }
    

提交回复
热议问题