iphone - UITableView 自定义单元格抛出 SIGABRT 错误

标签 iphone objective-c xcode ios5 uitableview

我正在尝试为我的 UITableView 创建自定义单元格。我正在使用 Xcode 4.2 并将 Storyboard与 ARC 一起使用。

我创建了一个类来表示自定义单元格,如下所示:

ResultsCustomCell.h

#import <UIKit/UIKit.h>

@interface ResultsCustomCell : UITableViewCell
{
    IBOutlet UILabel *customLabel;
}

@property (nonatomic, retain) IBOutlet UILabel* customLabel;

@end

ResultsCustomCell.m

#import "ResultsCustomCell.h"

@implementation ResultsCustomCell

@synthesize customLabel;

@end

然后我在我的 View Controller 中实现了 UITableView 方法,如下所示: ViewController.m

#import "ViewController.h"
#import "ResultsCustomCell.h"

@implementation ViewController

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

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

// Set the number of items in the tableview to match the array count
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{   
    return 5;
}

// Populate TableView cells with contents of array.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CellIdentifier";

    ResultsCustomCell *myCell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
    myCell.customLabel.text = @"helloWorld";

    return myCell;
}

// Define height for cell.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

@end

应用程序构建成功,但立即崩溃并给我以下错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

我错过了什么?

最佳答案

这是一个恼人的问题,因为调试器不会让您知道发生了什么。如果您单击“继续执行”,它会在出现异常时中断,然后简单地关闭。

这让我抓狂了 30 分钟,直到我右键单击自定义单元格中出现问题的 UILabel,这揭示了答案:单元格中的一个控件已经(或者已经,正如你显然修复的那样,也许没有注意到)一个虚假的导出。 “虚假 socket ”是指连接到您的类(class)中不再存在的 IBOutlet 属性的 socket 。在我的例子中,我更改了属性的名称并重新布线,但忘记删除旧的引用 socket 连接。

一旦我移除了有问题的 socket ,问题就消失了。

关于iphone - UITableView 自定义单元格抛出 SIGABRT 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9364523/

相关文章:

iphone - willRotateToInterfaceOrientation 未被调用

iphone - 使用 NSFileManager 的 copyItemAtURL 将图像从 iOS 设备的照片应用程序复制到我自己的应用程序目录

objective-c - NSSet 的迭代顺序是什么?

swift - 在新的 Firebase 和 Swift 中创建用户

ios - 以编程方式将自定义 TableView 创建为弹出窗口

javascript - Google 的 V8 JavaScript 引擎可用于 iOS 吗?

ios - 在应用程序中启用触摸 ID 和面容 ID

ios - Objective-C - ALAsset 和 XmlWriter 出现 EXC_BAD_ACCESS 错误

ios - Obj-C,如何迭代具有字体属性的 View 的所有 View ?

ios - HealthKit:每日平均心率