【问题标题】:UITableView implementation crashes the application but doesn't show an error in the codeUITableView 实现使应用程序崩溃,但在代码中没有显示错误
【发布时间】:2018-02-26 11:05:37
【问题描述】:

我需要帮助了解我的控制台返回错误的原因:

** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法在包中加载 NIB:“NSBundle (已加载)”,名称为“MyListingsTableViewCell” ** 首先抛出调用堆栈:...

我是使用这些原型单元格和表格的新手,我将附上下面的代码。感谢您提供的任何支持。

实现原型单元的视图控制器代码如下:

#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 类:

- (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
}

【问题讨论】:

标签: ios objective-c xcode uitableview


【解决方案1】:

如果您已经为表格视图单元格创建了 xib,那么您需要像这样注册..

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

如果您希望在情节提要中使用单元格,则需要指定其自定义类和单元格重用标识符...

当您使用自定义单元格时,您需要执行以下操作之一......

  • 创建一个xib并将nib注册到tableview

  • 在故事板中设计单元格,设置自定义类并重用单元格标识符。

你做了这两件事......这就是为什么它崩溃了,因为你正在将“MyListingsTableViewCell”笔尖注册到不退出的tableview。

像这样连接所有的 IBOutlest...

设置tableview委托和数据源...

或者您也可以通过代码在 MyListings 类中进行如下设置...

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

最终输出...从您的代码中可以正常工作...

【讨论】:

  • 然后使用[tableView dequeueReusableCellWithIdentifier:@"your cell identifier" forIndexPath:indexPath];重用单元格。
  • 我的代码中还没有这个吗?它显示“MyListingsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyListingsTableViewCell" forIndexPath:indexPath];"
  • 那你现在面临的问题是什么?
  • “由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无法在包中加载NIB:”
  • 您是否为单元格创建了 xib 或在情节提要中设计了单元格?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多