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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9364523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:22:29  来源:igfitidea点击:

UITableView Custom Cell Throwing SIGABRT Error

iphoneobjective-cxcodeios5uitableview

提问by HGomez

I am trying to create a custom cell for my UITableView. I am using Xcode 4.2 and using the storyboard along with ARC.

我正在尝试为我的 UITableView 创建一个自定义单元格。我正在使用 Xcode 4.2 并使用故事板和 ARC。

I have created a class to represent the custom cell like so:

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

ResultsCustomCell.h

结果CustomCell.h

#import <UIKit/UIKit.h>

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

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

@end

ResultsCustomCell.m

结果CustomCell.m

#import "ResultsCustomCell.h"

@implementation ResultsCustomCell

@synthesize customLabel;

@end

I have then implemented the UITableView method in my view controller as follows: ViewController.m

然后我在我的视图控制器中实现了 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

The application builds successfully but then crashes instantly and give me the following error:

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

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

What part am I missing out?

我错过了什么部分?

采纳答案by HGomez

I'm not entirely sure what I have done to resolve my problem but It is now working correctly.

我不完全确定我做了什么来解决我的问题,但它现在可以正常工作。

Here is what I currently have in my ViewController.m

这是我目前在 ViewController.m 中的内容

Note: The ResultsCustomCell.h and .m are the same as above.

注意: ResultsCustomCell.h 和 .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";

    // Make sure there are no quotations (") around the cell Identifier.
    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

I then made sure the following things where correct:

然后我确定以下几点是正确的:

  • The table view cell has the correct custom class (ResultsCustomCell).
  • The cell Identifier for the UITableViewCell was correct in Interface Builder.
  • The UILabel in interface builder was correctly linked to the IBOutlet.
  • The UITableView view had the correct controller (ViewController)
  • 表格视图单元格具有正确的自定义类 (ResultsCustomCell)。
  • UITableViewCell 的单元标识符在界面生成器中是正确的。
  • 界面构建器中的 UILabel 已正确链接到 IBOutlet。
  • UITableView 视图具有正确的控制器 (ViewController)

After using this code and checking all of the above it appears to work.

使用此代码并检查以上所有内容后,它似乎可以工作。

回答by sumizome

This is an irritating problem because the debugger gives you no clue as to what happened. It will break on the exception and then simply shut down if you click Continue Execution.

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

This drove me nuts for 30 minutes until I right clicked on a UILabel in the custom cell that was giving me problems, which revealed the answer: One of the controls in your cell has (or had, as you apparently fixed it, perhaps without noticing) a spurious outlet. By "spurious outlet" I mean one that is wired to an IBOutlet property that no longer exists in your class. In my case I had changed the name of the property and rewired it, but had forgotten to remove the old referencing outlet connection.

这让我发疯了 30 分钟,直到我右键单击了给我带来问题的自定义单元格中的 UILabel,这揭示了答案:您的单元格中的一个控件已经(或者有,因为您显然已经修复了它,也许没有注意到) 一个虚假的出口。“虚假插座”是指连接到您班级中不再存在的 IBOutlet 属性的插座。在我的情况下,我更改了属性的名称并重新连接了它,但忘记删除旧的参考插座连接。

As soon as I removed the offending outlet the problem went away.

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

回答by basvk

You forgot to create a new UITableViewCellwhen it's not dequeued

UITableViewCell当它没有出队时,你忘记创建一个新的

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

    ResultsCustomCell *myCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (myCell == nil) {
        myCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    myCell.customLabel.text = @"helloWorld";

    return myCell;
}

回答by jrturton

You have forgotten to add the reuse identifier ("CellIdentifier") in the storyboard against your prototype cell, so when it tries to create a new cell, there is no prototype with that identifier in the storyboard, and it returns nil.

您忘记在情节提要中针对原型单元格添加重用标识符(“CellIdentifier”),因此当它尝试创建新单元格时,情节提要中没有具有该标识符的原型,并返回nil.

回答by fscheidl

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

Automatic Reference Counting (ARC) forbids the explicit call of retain. Try to remove this.

自动引用计数 (ARC) 禁止显式调用retain. 尝试删除它。

As for the error, you do return a UITableViewCell, not your custom cell. Additionally, you never allocate your ResultsCustomCell.

至于错误,您确实返回了一个UITableViewCell,而不是您的自定义单元格。此外,您永远不会分配您的ResultsCustomCell.

- (ResultsCustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CellIdentifier";

    ResultsCustomCell *cell = (ResultsCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[ResultsCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell.
    cell.textLabel.text = @"Text";
    return cell;
}

Furthermore, your subclass of UITableViewCelldoes not declare the (obviously obligatory) method init.

此外,您的子类UITableViewCell没有声明(显然是强制性的)方法init

ResultsCustomCell.h:

结果CustomCell.h:

#import <UIKit/UIKit.h>

@interface ResultsCustomCell : UITableViewCell

@property (strong, nonatomic) UILabel *myLabel;

@end

ResultsCustomCell.m:

结果CustomCell.m:

#import "ResultsCustomCell.h"

@implementation ResultsCustomCell

@synthesize myLabel;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

@end

EDIT:I saw, that you are using storyboard. My suggestion might or might not be of help to you. I have never used storyboard.

编辑:我看到,您正在使用故事板。我的建议可能对你有帮助,也可能没有。我从未使用过故事板。

回答by KeithTheBiped

I created this exception by mistakenly registering the identifier with the wrong class type.

I created this exception by mistakenly registering the identifier with the wrong class type.

//The Identifier is register with the wrong class type
self.listView.register(CustomCellClass1.self, forCellReuseIdentifier: "CustomCellClass2")

And then dequeuing and casting to a different class type:

And then dequeuing and casting to a different class type:

let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCellClass2", for: indexPath) as! CustomCellClass2

回答by Henrik

I was also fighting this error with the same setup as yours. I realized that it, for me, was caused by the new "Auto layout" feature of iOS6. To disable it I opened the custom cell xib, and then on the right hand side Utilities I opened the File inspector tab (the tab to the far left). In there I could un-check "Use Autolayout".

I was also fighting this error with the same setup as yours. I realized that it, for me, was caused by the new "Auto layout" feature of iOS6. To disable it I opened the custom cell xib, and then on the right hand side Utilities I opened the File inspector tab (the tab to the far left). In there I could un-check "Use Autolayout".