iosuitableviewswiftuiviewawakefromnib

Trying to set a property inside a UIView before awakeFromNib is called


Im new to swift - i have 2 viewcontrollers and a segue bitween them. im trying to set a property inside the view before it is loaded. so this is some of the code from my uiview -

class ListUsersView: UIView {

  @IBOutlet var tableView: UITableView!  
  var dataSource: ListsUserDataSource?
  var nameForCellNib: String?

  override func awakeFromNib() {
      self.tableView.registerNib(UINib(nibName: nameForCellNib! , 
      bundle:NSBundle.mainBundle()), forCellReuseIdentifier: nameForCellNib!)
      ..
      ..
      ..
  }

Im having hard time setting nameForCellNib before the awakeFromNib (it is needed in awakeFromNib) thanks!


Solution

  • If you want to use a var as soon as you have it, you can use it's didSet observer like so:

    class ListUsersView: UIView {
    
        @IBOutlet var tableView: UITableView! {
            didSet{
                tableView.registerNib(UINib(nibName: nameForCellNib!, bundle:NSBundle.mainBundle()), forCellReuseIdentifier: nameForCellNib!)
            }
        }
        .
        .
        . 
    
    }