我对Objective C和iOS开发相当新,而且我仍然想要了解它。但我正在尝试使用故事板制作一个简单的iOS应用程序(截至目前)UITableViewController作为主页面。我只是想让细胞出现。我的代码是:
- (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 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cervical_patterns = @"cervical_patterns";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cervical_patterns"];
return cell;
}
我已经密切关注了第二个iOS观鸟教程应用(link),并且之前已经获得了一个可在模拟器中查看的UITableViewController。但我无法弄清楚为什么这不像观鸟应用程序那样。
我得到的错误是:
'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:
我做错了什么?
答案 0 :(得分:0)
所以你使用的是Storyboard和UITableViewController。这意味着你需要:
同时检查您是否正确设置了课程:
我刚刚在XCode中完成了这项工作并且完美运行(请注意,没有 (cell == nil)
代码是必需的)。下面的确切代码:
- (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 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cervical_patterns";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the text and graphics of the cell
return cell;
}
答案 1 :(得分:-1)
您需要实际创建单元格,如下所示:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cervical_patterns = @"cervical_patterns";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cervical_patterns"];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cervical_patterns ] autorelease];
}
return cell;
}