I am trying to use custom UITableViewCells defined in IB where there are referencing outlets. I have successfully used the techniques shown in several places in stackoverflow to load and use UITableViewClass when there is no referencing outlet, like below.
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"TheCellsClass" owner:nil options:nil];
I have a separate file called “TheCellsClass.xib”, which has a single UITableViewCell defined with a single UILable called Alabel, “IBOutlet UILabel *Alabel;”. If I connect the label to ALabel then I get this error
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x681b360> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Alabel.'
After searching here and the web I understand that this is caused by the fact that “owner:nil” does not define a class with this object:Alabel. I cannot use “owner:self” because that is the UITableViewController, and also does not define “Alabel”.
I created a class called “TheCellsClass” as a sub class of “UITableViewCell” that does define Alabel, see below;
Then used:
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"TheCellsClass" owner:cell options:nil];
I still get the same error. So, can anyone point out the error of my ways? 🙂
I only way I can think to fix this is to remove all referencing outlets and connect
them using code
Subclass header :
#import <UIKit/UIKit.h>
@interface TheCellsClass : UITableViewCell {
IBOutlet UILabel *Alabel;
}
@property (strong, nonatomic) UILabel *Alabel;
@end
Subclass body:
#import "TheCellsClass.h"
@implementation TheCellsClass
@synthesize Alabel;
@end
In the table view controller
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
I am using:
TheCellsClass* cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"TheCellsClass" owner:cell options:nil];
A zip of a sample project is here http://www.proaa.com/tryout.zip
Suggestions? Requests for more info?
Any help appreciated.
Geoff
Three things:
Remove TheCellClass from DetailViewController.xib
ALabel is linked to File’s Owner in TheCellsClass – link it instead to TheCellsClass in the Objects section. Also consider renaming ALabel – it is standard to name instance variables beginning with a lowercase letter.
In MasterViewController, change
to
After these three changes, your custom TableViewCell appeared in the simulator. Also consider renaming TheCellsClass to something that hints at what it’s subclassing, like MyCustomTableViewCell.