[[NSBundle mainBundle] loadNibnamed:owner:options:]崩溃

use*_*859 16 iphone xcode uitableview nib

我无法想出这个.我在IB中创建了一个自定义UITableViewCell.据我所知,我拥有单元格中的所有对象,这些对象都是正确连接的UILabel,所有内容都没有任何错误.当我尝试使用自定义单元格时,app会在尝试加载NIB时停止.我在下面列出了我的实际情况.

任何帮助或提示或在哪里寻找这一个将不胜感激.

谢谢,

约翰

这是我的UITableViewCell接口和实现方法.我不确定我是否真的需要为init分配标签.我试过两种方式......

//  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
Run Code Online (Sandbox Code Playgroud)

这是UITableView的cellForRowAtIndexPath的一部分

//  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;  
}
Run Code Online (Sandbox Code Playgroud)

程序执行在loadNibNamed行上停止,而控制台中没有任何注释.如果我"踏入"我得到以下内容,我不确定与崩溃的原因有什么关系,而是由于笔尖没有正确加载...

2011-03-29 12:18:14.137 JetLogger [4565:207] *由于未捕获的异常'NSUnknownKeyException'终止应用程序,原因:'[setValue:forUndefinedKey:]:此类不是密钥值的编码兼容的关键cityArrivalTimeLabel ".*第一次调用堆栈:(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主+ 102 28 JetLogger 0x00001b39开始+ 53)抛出'NSException'实例后终止调用

ugh*_*fhw 40

在您的笔尖中的某个位置,您将某些内容连接到没有该属性的对象上的cityArrivalTimeLabel属性.确保您1)将单元格的类更改为ArrivalTimesCell,并将该标签连接到右侧对象.

您不需要在init方法中分配/初始化标签.它们在笔尖加载时被简单地替换,因此在此之前它只是浪费RAM.

  • 问题是您将标签连接到文件的所有者,但是当您调用loadNibNamed ...时,您将nil作为文件的所有者传递.您应该将标签连接到笔尖中的单元格对象.initWithStyle:从nib加载时不会被调用.Nib加载使用initWithCoder:初始化对象并在加载nib后调用awakeFromNib. (13认同)