-5

我有这个目标 C 程序和错误打印输出:

#import <UIKit/UIKit.h>

@interface TableViewController : UITableViewController

@end

------


#import "TableViewController.h"

@interface TableViewController ()

@end

@implementation TableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

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

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1; 
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 50; 
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell...
    cell.textLabel.text = [NSString stringWithFormat:@"Zeile nummer %i",indexPath.row];
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}



@end

ErrorCode: 2012-09-16 21:05:36.204 renetest2[1176:fb03] * 断言失败 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-1914.84/UITableView.m:6061 2012-09-16 21:05:36.206 renetest2[1176:fb03] *由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“UITableView 数据源必须从 tableView:cellForRowAtIndexPath: 返回一个单元格:”*** 第一次抛出调用堆栈:(0x14b4022 0xeb4cd6 0x145ca48 0x9ae2cb 0xb1d28 0xb23ce 0x9dcbd 0xac6f1 0x55d21 0x14b5e42 0x1d85679 0x1d8f579 0x1d144f7 0x1d163f6 0x1da3160 0x15e84 0x16767 0x25183 0x25c38 0x19634 0x139eef5 0x1488195 0x13ecff2 0x13eb8da 0x13ead84 0x13eac9b 0x15c65 0x17626 0x1cad 0x1c15 0x1) terminate called throwing an exception(lldb)

4

3 回答 3

0

尝试了解错误并查看方法tableView:cellForRowAtIndexPath。而不是// Configure the cell...你应该分配/初始化你的单元格。

于 2012-09-16T19:36:20.073 回答
0

从上面的示例中,如果出队的单元格为 nil,则不会创建单元格。函数 cellForRowAtIndexPath 需要如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
    }

    // Configure the cell...
    cell.textLabel.text = [NSString stringWithFormat:@"Zeile nummer %i",indexPath.row];
    return cell;
}
于 2012-09-16T19:41:27.143 回答
0

错误在这里:

UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:

这意味着该行

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

无法将单元格实例出列,因此该方法正在返回nil.

您需要检查是否cell为 nil,然后创建一个新实例,如果它是:

if (nil == cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

然后,您将能够配置新UITableViewCell实例。

于 2012-09-16T19:43:49.480 回答