How do I create a custom UITableViewCell with nib in Xcode 4?

12,627

Solution 1

This blog post describes, how to add it manually.

in iOS4+, the line

NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@”CustomCell” owner:self options:nil];

can be replaced by a more efficient nib handling via UINib.
see this blog post how to do it, and how to stay backwards compatible.

Solution 2

As I understood you already have .h/.m files and you are missing the view (nib).

  1. Please select new file, select "User Interface" category and empty in sub category.
  2. Once your nib is created, drag and drop the UITableViewCell on the screen.
  3. Set the class as First Responder.
Share:
12,627
TigerCoding
Author by

TigerCoding

Tiger Coding provides mobile application consulting and development.

Updated on June 16, 2022

Comments

  • TigerCoding
    TigerCoding almost 2 years

    In Xcode 3, I could elect to make a nib when creating a table view cell subclass. In Xcode 4, it only makes the h / m files.

    How do I make the subclass with a xib file?

    Edit: see screen shots below:

    New File

    enter image description here

    EDIT: Additionally, is there any way to create the UITableViewCell nib automatically, along with the h and m files, all pre-wired and ready to edit?

    • TigerCoding
      TigerCoding over 12 years
      Where? When I select: File, New File, Cocoa Touch, Objective-C Class, Next, (type class name), select UITableViewCell from drop down list, Next...it then goes to the create dialog. Nowhere is an option to check with xib for user interface.
    • rohan-patel
      rohan-patel over 12 years
      Not getting question proprly..can you please explain your question inmore simpler way?So maybe I can help.
    • TigerCoding
      TigerCoding over 12 years
      Still don't see where or how to create the nib file in XCode 4...
    • Kjuly
      Kjuly over 12 years
    • Kjuly
      Kjuly over 12 years
      Create xib(nib) file: File->New->New File->User Interface->View.
    • Tendulkar
      Tendulkar over 12 years
      This link may bel helpful to you.
  • TigerCoding
    TigerCoding over 12 years
    Great link, but is there a way to not do it manually? Or is that the only way?
  • TigerCoding
    TigerCoding over 12 years
    Close, but step three is incorrect (see @vikingosegundo link). If I don't have the h / m files, can I make the nib, h and m files all at once somehow (All pre-wired)?
  • vikingosegundo
    vikingosegundo over 12 years
    It is the only way I ever needed. and a quite elegant one compared to others, that uses xibs without a proper file owner.
  • TigerCoding
    TigerCoding over 12 years
    Are all the default connections are wired already, such as backgroundView and contentView, or do I need to manually connect them? What about textLabel?
  • vikingosegundo
    vikingosegundo over 12 years
    Actually I am not sure, if this connection gets established, as you are not passing in a certain cellStyle. Try it out!
  • TigerCoding
    TigerCoding over 12 years
    Doing it the way its shown sets it up as a UITableViewCell, so it still keeps its properties. The first link does have an error though, it should be: @property (nonatomic, retain) IBOutlet UITableViewCell *customCell;