I have a custom static UITableViewCell
that I want to look exactly the same as a right detail cell (UILabel
on the left, UILabel
on the right), except I want the right UILabel
to be an editable UITextField
. Since I want to use this cell in multiple view controllers in my storyboard, I decided the best option would be to create a MyEditableTableViewCell.xib file, along with corresponding *.h,m files. I have two public properties in MyEditableTableViewCell.h:
@property (weak, nonatomic) IBOutlet UILabel *textLabel;
@property (weak, nonatomic) IBOutlet UITextField *detailTextField;
These IBOutlet
s are connected to the UILabel
and UITextField
in the .xib file. In my storyboard, where I have a custom UITableViewController
subclass, I change the class of the necessary UITableViewCell
to MyEditableTableViewCell
. I have a property in my view controller that is:
@property (weak, nonatomic) IBOutlet MyEditableTableViewCell *myCell;
However, when I run the app, the cell appears as simply a blank cell. Running some checks on it, I see that [myCell isKindOfClass:[MyEditableTableViewCell class]]
returns true. However, the UITextField
never seems to get instantiated. When I try to alter myCell.detailTextField.text
, nothing happens, and myCell.detailTextField
appears to be nil.
Any help would be appreciated!
The same way that you create a UIViewController or UITableViewController : In your project, go to New File. Select Cocoa Touch Class under iOS. The subclass is UITableViewCell and the name is whatever name you're giving it. Within that class, you drag your buttons, labels, etc to create your outlets.
How are you creating instances of MyEditableTableViewCell
? My guess is you're doing [[MyEditableTableViewCell alloc] init]
instead of loading them from the nib.
You don't need that property in your view controller. You need to register your cell nib with the table view using -[UITableView registerNib:forCellReuseIdentifier:]
. Do that in your viewDidLoad
. Then, when you send dequeueReusableCellWithIdentifier:
to the table view, it will instantiate the nib, creating a MyEditableTableViewCell
, and return it to you.
If you're laying out static cells in a storyboard, using a xib to lay out a cell is somewhat difficult. The simplest thing to do is simply lay out the cell's subviews in the storyboard. You can copy and paste the subviews to each cell if you have a few of the same type.
If you really want to use a xib, the easiest way is to structure your cell's view hierarchy like this:
MyEditableTableViewCell (in storyboard)
|
+- cell's content view (created automatically by UIKit)
|
+- UIView (top-level view of the xib)
|
+- UILabel (textLabel)
|
+- UITextField (detailTextField)
So you set the cell class in the storyboard to MyEditableTableViewCell
. Then you create your xib. You set your xib File's Owner class to MyEditableTableViewCell
. The xib does not contain a MyEditableTableViewCell
. The top-level view of the xib is just a plain UIView
, containing the subviews (the label and the text field).
In -[MyEditableTableViewCell initWithCoder:]
, after doing self = [super initWithCoder:aDecoder]
, instantiate the xib, passing self
(the cell) as the xib file's owner. The cell can have outlets connected to the label and the text field in the xib.
After instantiating the xib, add the top-level view from the xib as a subview of self.contentView
. If you're using auto layout, create constraints betwixt the top-level view and the content view. Otherwise, set the top-level view's frame
to the content view's bounds
and set the autoresizing mask to UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With