Xcode 5.1 UITableView in UIViewController - Custom TableViewCell Outlets are nil

后端 未结 2 1515
借酒劲吻你
借酒劲吻你 2021-02-06 17:10

I have a UITableView as a subview in a UIViewController. I\'m not using UITableViewController because I have some other content that is not tableview-related taking up part of t

2条回答
  •  时光说笑
    2021-02-06 17:51

    Here are some tips:

    Basically there can be 3 ways to create and use UITableViewCells:

    1. Use “prototype” cells in the storyboard/nib

      (Create cell UI in the storyboard/nib inside UITableView)

      You should not use either registerClass:forCellReuseIdentifier: or registerNib:forCellReuseIdentifier: methods. UIStoryboard handles it on its own, since you specified the reuseIdentifier in the storyboard, the tableView knows which cell is associated with which reuseIdentifier.

    2. Use a separate nib for tableViewCell UI

      (This might be a way to go when you want to use the same tableViewCell in several tableViews)

      You should use registerNib:forCellReuseIdentifier: method.

    3. Do not use nibs, create tableViewCell UI programmatically

      You should use registerClass:forCellReuseIdentifier: method.

    Also, note that both registerClass:forCellReuseIdentifier: and registerNib:forCellReuseIdentifier: should be called before (most often in viewDidLoad) dequeueReusableCellWithIdentifier:forIndexPath:method is called. If you registered a class or a nib for the reuseIdentifier it is guaranteed that dequeueReusableCellWithIdentifier:forIndexPath: returns a valid cell (unlike dequeueReusableCellWithIdentifier:).

    In your code Storyboard registered a nib with UI for the cell, but you calling registerClass:forCellReuseIdentifier: with the same reuseIdentifier forced table view to unregister the nib and register the class which was not aware of IBOutlets.

提交回复
热议问题