简体   繁体   中英

UITableView implementation crashes the application but doesn't show an error in the code

I need help understanding why my console returns the error:

** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle <--APP_LOCATION--> (loaded)' with name 'MyListingsTableViewCell'' ** First throw call stack: ...

I'm new to using these prototype cells and tables, I will attach the code below. I appreciate any support that you can offer.

Code for the view controller where the prototype cell is implemented as follow:

#import "MyListings.h"

@interface MyListings ()

@property (weak, nonatomic) IBOutlet UIButton *createListingButton;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation MyListings

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"MyListingsTableViewCell"];

    UINib *nib1 = [UINib nibWithNibName:@"MyListingsTableViewCell" bundle:nil];
    [self.tableView registerNib:nib1 forCellReuseIdentifier:@"MyListingsTableViewCell"];

    titles = [[NSArray alloc]initWithObjects:@"1",@"2", nil];
    currentBids = [[NSArray alloc]initWithObjects:@"3",@"4", nil];
    stillAvailables = [[NSArray alloc]initWithObjects:@"5",@"6", nil];

    _createListingButton.layer.cornerRadius = 8;
    _createListingButton.layer.borderWidth = 1.5f;
    _createListingButton.layer.borderColor = [UIColor whiteColor].CGColor;
    [_createListingButton addTarget:self action:@selector(createListingButtonHighlightBorder) forControlEvents:UIControlEventTouchDown];
    [_createListingButton addTarget:self action:@selector(createListingButtonUnhighlightBorder) forControlEvents:UIControlEventTouchUpInside];
    [_createListingButton addTarget:self action:@selector(createListingButtonUnhighlightBorder) forControlEvents:UIControlEventTouchDragExit];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)createListingButtonHighlightBorder
{
    _createListingButton.layer.borderColor = [UIColor colorWithRed:0.61 green:0.00 blue:0.02 alpha:1.0].CGColor;
}

- (void)createListingButtonUnhighlightBorder
{
    _createListingButton.layer.borderColor = [UIColor whiteColor].CGColor;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [titles count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyListingsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyListingsTableViewCell" forIndexPath:indexPath];
    [cell updateCellWithTitle:[titles objectAtIndex:indexPath.row] currentBid:[currentBids objectAtIndex:indexPath.row] stillAvailable:[stillAvailables objectAtIndex:indexPath.row]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}

Prototype Cell class:

- (void)awakeFromNib
{
    [super awakeFromNib];
    // Initialization code
}

- (void)updateCellWithTitle:(NSString *)title currentBid:(NSString *)bid stillAvailable:(NSString *)available
{
    self.titleLabel.text = title;
    self.currentBidLabel.text = bid;
    self.stillAvailableLabel.text = available;
    self.editLabel.text = @"Press to edit";
}

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

    // Configure the view for the selected state
}

if you have created a xib for table view cell then you need to register like this..

UINib *nib1 = [UINib nibWithNibName:@"MyListingsTableViewCell" bundle:nil];
[self.tableView registerNib:nib1 forCellReuseIdentifier:@"MyListingsTableViewCell"];

If you have desired you cell in storyboard then you need to specify its custom class and cell reuse identifier...

在此处输入图片说明

在此处输入图片说明

When every you are working with custom cells then you need to one of the following....

  • Create a xib and register nib to tableview

or

  • Design cell in story board, set custom class and reuse cell identifier.

You did both the things...thats why it was crashing bcoz you are registering "MyListingsTableViewCell" nib to tableview that does not exits.

Connect all IBOutlest like this...

在此处输入图片说明

Set tableview delegate and datasource...

在此处输入图片说明

Or from code also you can set this like below in your MyListings class...

self.tableView.dataSource = self
self.tableView.delegate = self

Final output...its working fine from your code...

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM