使用简单的故事板应用程序获取SIGABRT错误
我遇到了一个问题,我在网上搜索了解决方案,但无济于事。我正沿着Apress'开始在故事板上开始iOS 5开发的第10章。使用简单的故事板应用程序获取SIGABRT错误
我在想这本书缺少一段简单的代码,而且由于我第一次经历这个,所以我不知道如何解决它。
我已经重新启动该项目两次,但不断收到此错误调试器:
终止应用程序由于未捕获的异常“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
缺乏代码:
...
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.任何想法? – Devj 2012-04-23 22:54:09
[cell addSubview:label];然后添加代码:if([identifier isEqualToString:@“attentionCell”]) label.textColor = [UIColor redColor]; – jessex 2012-04-24 01:39:59
我不能说谢谢!惊讶的是,这本书有很多代码。现在一切顺利!疯狂的道具! – Devj 2012-04-24 02:07:15
事实上,我认为你原来的问题没有什么从代码丢失,它是从你的故事板的配置丢失! IIRC,本书描述了在故事板中创建两个单元格原型:一个带有红色文本,其标识符设置为“attentionCell”,另一个带有黑色文本并将其标识符设置为“plainCell”。如果这两个单元格存在于故事板的tableview中,并且设置了正确的标识符,那么您发布的原始代码应该可以正常工作。
没有缺失代码。我完全按照第10章中的说明进行编译并且运行良好。
页面顶部的第363页解释了从故事板加载的表视图可以按需创建单元格,并且不需要检查无返回值。
您需要添加UITableViewDataSource在接口文件 – 2012-04-22 16:40:56
增加一条,作为我在执行协议和头文件,像这样:@interface BIDTaskListController:UITableViewController中这并没有工作...我也有我的应用程序代理.h和.m; bidviewcontroller.h和.m;我应该添加什么吗? –
Devj
2012-04-22 20:02:15