问题描述:

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:

  1. Remove TheCellClass from DetailViewController.xib

  2. 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.

  3. In MasterViewController, change

    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"TheCellsClass" owner:cell options:nil];
    

to

    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"TheCellsClass" owner:nil options:nil];

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.

网友答案:

Without looking at your project, one thing comes to mind. If you have an instance of your custom cell in our nib, and then you have this label in that same nib, then the connection should be from the UILabel to the custom cell. You shouldn't be connecting it to file's owner, at least from the way you've described what you're trying to achieve.

Hope that helps.

相关阅读:
Top