【问题标题】:Getting SIGABRT error with simple storyboard app使用简单的故事板应用程序获取 SIGABRT 错误
【发布时间】:2012-05-03 10:16:44
【问题描述】:

我有一个问题,我在互联网上搜索了解决方案,但无济于事。我在故事板上的 Apress 开始 iOS 5 开发的第 10 章。

我认为这本书缺少一段简单的代码,因为我是第一次阅读,所以我不知道如何解决它。

我已经重新启动了两次项目,但在调试器中不断收到此错误:

由于未捕获的异常“NSInternalInconsistencyException”而终止应用,原因:“UITableView 数据源必须从 tableView:cellForRowAtIndexPath: 返回一个单元格:”

#import "BIDTaskListController.h"

@interface BIDTaskListController ()
@property (strong, nonatomic) NSArray *tasks;
@end

@implementation BIDTaskListController

@synthesize tasks;

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tasks = [NSArray arrayWithObjects:
                  @"Walk the dog",
                  @"URGENT:Buy milk",
                  @"Clean hidden lair",
                  @"Invent miniature dolphins",
                  @"Find new henchmen",
                  @"Get revenge on do-gooder heroes",
                  @"URGENT: Fold laundry",
                  @"Hold entire world hostage",
                  @"Manicure",
                  nil];
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;

    self.tasks = nil;
}

//

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [tasks count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *identifier = nil;
    NSString *task = [self.tasks objectAtIndex:indexPath.row];
    NSRange urgentRange = [task rangeOfString:@"URGENT"];
    if (urgentRange.location == NSNotFound) {
        identifier = @"plainCell";
    } else {
        identifier = @"attentionCell";
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    UILabel *cellLabel = (UILabel *)[cell viewWithTag:1];
    cellLabel.text = task;

    return cell;
}
@end

【问题讨论】:

  • 你需要在你的界面文件中添加 UITableViewDataSource
  • 将它作为协议添加到我的实现和头文件中,如下所示:@interface BIDTaskListController : UITableViewController 这不起作用...我还有我的应用程序 delegate.h 和.m; bidviewcontroller.h 和 .m;我应该向他们添加什么吗?

标签: ios uitableview sigabrt uncaught-exception


【解决方案1】:

缺少代码:

...

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    UILabel *label = [[UILabel alloc] initWithFrame:cell.bounds];
    label.tag = 1;
    [cell addSubview:label];
}

UILabel *cellLabel = (UILabel *)[cell viewWithTag:1];
cellLabel.text = task;   

....

【讨论】:

  • 太棒了!这带来了表格视图。不过有一件小事:我有一个表格视图,里面有两个视图单元格,每个单元格都包含它们自己的标签。一个标签仅用于应显示为红色的紧急响应。当我运行应用程序时,它们看起来就像其他数据一样。我将文本颜色设置为红色,并将视图标记设置为 1。有什么想法吗?
  • [单元格 addSubview:label];然后添加代码: if ([identifier isEqualToString:@"attentionCell"]) label.textColor = [UIColor redColor];
  • 感激不尽!很惊讶这本书里有这么多代码。现在一切都很顺利!疯狂的道具!
【解决方案2】:

实际上,我认为您的原始问题不是代码中缺少任何内容,而是您的情节提要配置中缺少! IIRC,这本书描述了在故事板中创建两个单元原型:一个带有红色文本,其标识符设置为“attentionCell”,一个带有黑色文本,其标识符设置为“plainCell”。如果这两个单元格存在于情节提要的 tableview 中,并且设置了正确的标识符,那么您发布的原始代码应该可以正常工作。

【讨论】:

    【解决方案3】:

    没有丢失的代码。我完全按照第 10 章中的说明进行操作,编译并运行良好。
    363页顶部解释了从storyboard加载的table view可以按需创建cell,不需要检查nil return。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-03
      • 2013-01-11
      • 2012-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-07
      • 2016-05-31
      相关资源
      最近更新 更多