16

I cannot figure this one out. I have a created a custom UITableViewCell in IB. As far as I can tell I have all the objects in the cell which are all UILabels wired up properly and everything builds without any errors. When I try to use the custom cell the app stops when I try to load the NIB. I've listed what I have in place for this below.

Any help or hints or where to look to figure this one out would be appreciated.

Thanks,

John

Here are my UITableViewCell interface and implementation methods. I am not sure I really needed to alloc init the labels. I tried it both ways...

//  ArrivalTimesCell.h

#import <UIKit/UIKit.h>

@interface ArrivalTimesCell : UITableViewCell {
    IBOutlet UILabel *cityArrivalTimeLabel;
    IBOutlet UILabel *optimumArrivalTimeLabel;
    IBOutlet UILabel *arrivalAvoidTimesLabel;
    IBOutlet UILabel *arrivalTimeNotSetLabel;

}
@property (nonatomic, retain)IBOutlet UILabel *cityArrivalTimeLabel;
@property (nonatomic, retain)IBOutlet UILabel *optimumArrivalTimeLabel;
@property (nonatomic, retain)IBOutlet UILabel *arrivalAvoidTimesLabel;
@property (nonatomic, retain)IBOutlet UILabel *arrivalTimeNotSetLabel;

@end

//  ArrivalTimesCell.m

#import "ArrivalTimesCell.h"

@implementation ArrivalTimesCell
@synthesize arrivalTimeNotSetLabel, arrivalAvoidTimesLabel, optimumArrivalTimeLabel, cityArrivalTimeLabel;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        arrivalTimeNotSetLabel = [[UILabel alloc] init];
        arrivalAvoidTimesLabel = [[UILabel alloc] init];
        optimumArrivalTimeLabel = [[UILabel alloc] init];
        cityArrivalTimeLabel = [[UILabel alloc] init];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated{
    [super setSelected:selected animated:animated];

}

- (void)dealloc{
    [super dealloc];
    [arrivalTimeNotSetLabel release];
    arrivalTimeNotSetLabel = nil;
    [arrivalAvoidTimesLabel release];
    arrivalAvoidTimesLabel = nil;
    [optimumArrivalTimeLabel release];
    optimumArrivalTimeLabel = nil;
    [cityArrivalTimeLabel release];
    cityArrivalTimeLabel = nil;
}

@end

Here is a portion of cellForRowAtIndexPath for the UITableView

//  TripEditViewController.m

#import "ArrivalTimesCell.h"

@implementation TripEditViewController

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    CellIdentifier = @"ArrivalTimesCell";
    ArrivalTimesCell *cell = (ArrivalTimesCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil){
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ArrivalTimesCell" owner:nil options:nil];

        for (id currentObject in topLevelObjects) {
            if ([currentObject isKindOfClass:[UITableViewCell class]]) {
                cell = (ArrivalTimesCell *) currentObject;
                break;
             }
         }      
    }


    cell.cityArrivalTimeLabel.text = @"Whatever"
    cell.arrivalAvoidTimesLabel.text = @"Whatever";
    cell.optimumArrivalTimeLabel.text = @"Whatever";
    cell.arrivalTimeNotSetLabel.text = @"Whatever";

    return cell;  
}

Program execution stops on the loadNibNamed line without any comment in the console. If I "step into" I get the following which I am not sure has anything to do with the cause of my crash but instead is the result of the nib not loading properly...

2011-03-29 12:18:14.137 JetLogger[4565:207] * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key cityArrivalTimeLabel.' * Call stack at first throw: ( 0 CoreFoundation 0x0111a5a9 exceptionPreprocess + 185 1 libobjc.A.dylib 0x0126e313 objc_exception_throw + 44 2 CoreFoundation 0x0111a4e1 -[NSException raise] + 17 3 Foundation 0x000ca677 _NSSetUsingKeyValueSetter + 135 4 Foundation 0x000ca5e5 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 285 5 UIKit 0x0054e30c -[UIRuntimeOutletConnection connect] + 112 6 CoreFoundation 0x010908cf -[NSArray makeObjectsPerformSelector:] + 239 7 UIKit 0x0054cd23 -[UINib instantiateWithOwner:options:] + 1041 8 UIKit 0x0054eab7 -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] + 168 9 JetLogger 0x0004bf18 -[TripEditViewController tableView:cellForRowAtIndexPath:] + 699 10 UIKit 0x003c7b98 -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 634 11 UIKit 0x003bd4cc -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] + 75 12 UIKit 0x003d28cc -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1561 13 UIKit 0x003ca90c -[UITableView layoutSubviews] + 242 14 QuartzCore 0x02051a5a -[CALayer layoutSublayers] + 181 15 QuartzCore 0x02053ddc CALayerLayoutIfNeeded + 220 16 QuartzCore 0x01ff90b4 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310 17 QuartzCore 0x01ffa294 _ZN2CA11Transaction6commitEv + 292 18 QuartzCore 0x01ffa46d _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99 19 CoreFoundation 0x010fb89b __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 27 20 CoreFoundation 0x010906e7 __CFRunLoopDoObservers + 295 21 CoreFoundation 0x010591d7 __CFRunLoopRun + 1575 22 CoreFoundation 0x01058840 CFRunLoopRunSpecific + 208 23 CoreFoundation 0x01058761 CFRunLoopRunInMode + 97 24 GraphicsServices 0x01a081c4 GSEventRunModal + 217 25 GraphicsServices 0x01a08289 GSEventRun + 115 26 UIKit 0x00360c93 UIApplicationMain + 1160 27 JetLogger 0x00001ba8 main + 102 28 JetLogger 0x00001b39 start + 53 ) terminate called after throwing an instance of 'NSException'

user278859
  • 10,379
  • 12
  • 51
  • 74

1 Answers1

42

Somewhere in your nib, you connect something to the cityArrivalTimeLabel property on an object that does not have that property. Make sure you 1) changed the class of the cell to ArrivalTimesCell and 2) connected that label to the right object.

You do not need to alloc/init the labels in your init method. They are simply replaced when the nib loads, so it is just a waste of RAM until then.

ughoavgfhw
  • 39,734
  • 6
  • 101
  • 123
  • OK, thanks. I rechecked and in the NIB both File's Owner and the UITableViewCell has their Class popup selected to ArrivalTimesCell. I have also double checked that the four UILabels are in act UILabels and are connected to the prober properties in ArrivalTimesCell.h (file's Owner). I am going to recreate the xib from scratch. Perhaps that will help. – user278859 Mar 30 '11 at 07:55
  • Still does not work. Do you think the NIB is loading at all? I put a break in the initWithStyle method of ArrivalsTimeCell.m and it never breaks. – user278859 Mar 30 '11 at 09:31
  • 13
    The problem is that you connected the labels to the File's Owner, but when you call loadNibNamed..., you pass nil as the file's owner. You should connect the labels to the cell object in the nib. initWithStyle: will not be called when loading from a nib. Nib loading uses initWithCoder: to initialize the object and calls awakeFromNib after the nib is loaded. – ughoavgfhw Mar 30 '11 at 20:47
  • Thanks, that indeed was the problem. I had tried connecting both ways but when I had it connected correctly with the cell I got an error which I did not read assuming it was the same error, which it was not. The new error occurs when I return the cell and is.. "UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:". The cell looks to be initialized with the 4 labels properly set just before I return it. If you have any hints as to what I should do to fix the new problem that would be great. – user278859 Mar 31 '11 at 01:36
  • Got it. There are actually 2 tables being handled by the view controller with an if statement in the cellForRowAtIndexPath and I was returning the cell with a single call outside of the if statement. I changed this to return the cell for each table inside the if statement and it is now working great. Thanks for your help! – user278859 Mar 31 '11 at 02:39