我有一个带按钮的视图控制器。当用户点击按钮时,我希望用户被导航到表视图控制器(即显示为表视图)。
单击按钮时,我可以调用以下方法:
- (void)loadTableViewController{
TableViewViewController *tableViewController = [[TableViewController alloc] initWithNibName:nil bundle:NULL];
[self.navigationController pushViewController:tableViewController animated:YES];
}
上面的代码似乎很好,因为在最后一行之后的调试模式中,我被带到TableView Controller的实现文件中。事情发生了......
我将tableView
声明为tableViewController
以下是viewDidLoad方法的代码:
[super viewDidLoad];
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
[self.view addSubview:self.tableView];
程序在调试模式的最后一行之后发生故障。根本不确定是什么问题...我所做的唯一其他改变是返回1个部分和1行表。这是代码:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = @"Test";
return cell;
}
编辑:添加错误详细信息。
我得到的错误看起来像一个通用的“libc ++ abi.dylib:以NSException类型的未捕获异常终止 (lldb)“
请记住,我对Xcode和编程很新。所以我可能没有找到找到具体错误的正确位置。
答案 0 :(得分:1)
您没有使用UITableViewController's
初始化方法。相反,使用
TableViewViewController *tableViewController = [[TableViewController alloc] initWithStyle:UITableViewStylePlain];
[self.navigationController pushViewController:tableViewController animated:YES];
之后,在UITableViewController
课程中初始化您的表格视图毫无意义,因为UITableViewController
已经为您做了(这是使用它),而是访问{{1}带有tableView
的属性。
另请注意,self.tableView
仅适用于iOS 6.0及更高版本(将在此之下崩溃)
填充单元格只需使用:
- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier
答案 1 :(得分:0)
我的猜测是问题是由这一行引起的:
TableViewViewController *tableViewController = [[TableViewController alloc] initWithNibName:nil bundle:NULL];
您缺少实际的笔尖名称。用你的笔尖名称替换nil。