【问题标题】:UITableView Custom Cell Throwing SIGABRT ErrorUITableView 自定义单元格抛出 SIGABRT 错误
【发布时间】:2012-03-10 23:51:15
【问题描述】:

我正在尝试为我的 UITableView 创建一个自定义单元格。我正在使用 Xcode 4.2 并将情节提要与 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

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

我错过了什么?

【问题讨论】:

标签: iphone objective-c xcode ios5 uitableview


【解决方案1】:

我错误地使用错误的类类型注册了标识符,从而创建了这个异常。

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

然后出队并转换为不同的类类型:

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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

    • 嘘。我不敢相信我只是这样做了。谢天谢地,你的答案突然出现了。谢谢!
    • 这些每次都能吸引我!
    • 非常有用的答案。出乎意料!
    • 2019,故事板仍然存在同样的问题^^
    【解决方案3】:

    我也使用与您相同的设置来解决此错误。我意识到,对我来说,这是由 iOS6 的新“自动布局”功能引起的。要禁用它,我打开了自定义单元格 xib,然后在右侧的实用程序中打开了文件检查器选项卡(最左侧的选项卡)。在那里我可以取消选中“使用自动布局”。

    【讨论】:

      【解决方案4】:

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

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

      注意: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
      

      然后我确保以下事情正确:

      • 表格视图单元格具有正确的自定义类 (ResultsCustomCell)。
      • UITableViewCell 的单元格标识符在 Interface Builder 中是正确的。
      • 界面生成器中的 UILabel 已正确链接到 IBOutlet。
      • UITableView 视图具有正确的控制器 (ViewController)

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

      【讨论】:

        【解决方案5】:

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

        【讨论】:

        • 我已放置重用标识符 (CellIdentifier),但仍收到此错误。
        • 在你的 cellForRowAtIndexPath 方法中放置一个断点,它会被调用吗?您是否在情节提要中将表格视图控制器的自定义类设置为ViewController?如果你没有,你将调用 cellForRow 的基本实现......它什么都不做。
        • 是的,我已经在 tableView 控制器上正确设置了自定义类。我还放置了一个断点,并且似乎正确调用了 cellForRowAtIndexPath。方法失败就行了。 myCell.customLabel.text = @"helloWorld";
        【解决方案6】:
        @property (nonatomic, retain) IBOutlet UILabel* customLabel;
        

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

        至于错误,您确实返回了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;
        }
        

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

        ResultsCustomCell.h:

        #import <UIKit/UIKit.h>
        
        @interface ResultsCustomCell : UITableViewCell
        
        @property (strong, nonatomic) UILabel *myLabel;
        
        @end
        

        ResultsCustomCell.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
        

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

        【讨论】:

          【解决方案7】:

          您忘记在未出列时创建新的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;
          }
          

          【讨论】:

          • 你不要用storyboard做这个,如果没有,dequeue方法会为你创建并返回一个单元格。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-07-17
          • 1970-01-01
          相关资源
          最近更新 更多