无法在捆绑中加载NIB:' NSBundle'在ios?

时间:2016-11-23 12:05:27

标签: ios objective-c uitableview

我的应用中包含tableview的视图。我使用 cellForRowAtIndexPath 函数填充tableview,但有时会崩溃并抛出如下错误。它很少发生,但我想解决它!

代码部分:

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

    if (self.productViewType == ProductToShelfViewTypeShelf) {

        static NSString* cellIdentifier = @"ShelfCell";
        ShelfCellView* cell = (ShelfCellView*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            NSArray* nibObjects = [[NSBundle mainBundle] loadNibNamed:@"ShelfCellView" owner:nil options:nil];
            for (id currentObject in nibObjects) {
                if ([currentObject isKindOfClass:[ShelfCellView class]]) {
                    cell = (ShelfCellView*)currentObject;
                }
            }
            [cell initViewStyles];
        }

        Shelf* shelf = [self.shelfList objectAtIndex:indexPath.row];
        cell.shelf = shelf;
        [cell initView];
        return cell;
    }
    else {
        static NSString* cellIdentifier = @"ProductToShelfCell";
        ProductToShelfCellView* cell = (ProductToShelfCellView*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            NSArray* nibObjects = [[NSBundle mainBundle] loadNibNamed:@"ProductToShelfCellView" owner:nil options:nil];
            for (id currentObject in nibObjects) {
                if ([currentObject isKindOfClass:[ProductToShelfCellView class]]) {
                    cell = (ProductToShelfCellView*)currentObject;
                }
            }
            [cell initViewStyles];
        }
        Product* product = [self.products objectAtIndex:indexPath.row];
        cell.product = product;
        [cell initView];
        return cell;
    }

    return nil;
}

Stacktrace:

Could not load NIB in bundle: 'NSBundle </var/containers/Bundle/Application/B07CBD90-32C2-4F41-ADB0-11AA6422AFF3/Kitapyurdu.app> (loaded)' with name 'ProductToShelfCellView'
    Fatal Exception: NSInternalInconsistencyException
    0  CoreFoundation                 0x1f576e07 __exceptionPreprocess
    1  libobjc.A.dylib                0x1e7d7077 objc_exception_throw
    2  CoreFoundation                 0x1f576d4d -[NSException initWithCoder:]
    3  UIKit                          0x24aa5007 -[UINib instantiateWithOwner:options:]
    4  UIKit                          0x24aa726b -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:]
    5  Kitapyurdu                     0x50321 -[ProductToShelfViewController tableView:cellForRowAtIndexPath:] (ProductToShelfViewController.m:429)
    6  UIKit                          0x249027fb -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:]
    7  UIKit                          0x249029e5 -[UITableView _createPreparedCellForGlobalRow:willDisplay:]
    8  UIKit                          0x248ef1ef -[UITableView _updateVisibleCellsNow:isRecursive:]
    9  UIKit                          0x24907ae1 -[UITableView _performWithCachedTraitCollection:]
    10 UIKit                          0x246ad345 -[UITableView layoutSubviews]
    11 UIKit                          0x245c65d5 -[UIView(CALayerDelegate) layoutSublayersOfLayer:]
    12 QuartzCore                     0x223ec109 -[CALayer layoutSublayers]
    13 QuartzCore                     0x223e031f CA::Layer::layout_if_needed(CA::Transaction*)
    14 QuartzCore                     0x223e01af CA::Layer::layout_and_display_if_needed(CA::Transaction*)
    15 QuartzCore                     0x22370a6b CA::Context::commit_transaction(CA::Transaction*)
    16 QuartzCore                     0x2238f035 CA::Transaction::commit()
    17 UIKit                          0x2484891f _UIApplicationFlushRunLoopCATransactionIfTooLate
    18 UIKit                          0x24d6388f __handleEventQueue
    19 CoreFoundation                 0x1f532c8b __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__
    20 CoreFoundation                 0x1f532795 __CFRunLoopDoSources0
    21 CoreFoundation                 0x1f530a6b __CFRunLoopRun
    22 CoreFoundation                 0x1f480073 CFRunLoopRunSpecific
    23 CoreFoundation                 0x1f47fe81 CFRunLoopRunInMode
    24 GraphicsServices               0x20c2cbfd GSEventRunModal
    25 UIKit                          0x2463482f -[UIApplication _run]
    26 UIKit                          0x2462ef61 UIApplicationMain
    27 Kitapyurdu                     0x222fb main (main.m:16)
    28 libdispatch.dylib              0x1ec4a50b (Missing)

1 个答案:

答案 0 :(得分:0)

在.m文件中添加以下代码,对我有用

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    NSBundle *bundel = [NSBundle bundleForClass:[Yourclassname class]];

    self = [super initWithNibName:nibNameOrNil bundle:bundel];
    if (self) {

        // Custom initialization
    }
    return self;
}