Objective c 表视图控制器测试程序

Objective c 表视图控制器测试程序,objective-c,ios,uitableview,Objective C,Ios,Uitableview,我有这个目标C程序和错误打印输出: #import <UIKit/UIKit.h> @interface TableViewController : UITableViewController @end ------ #import "TableViewController.h" @interface TableViewController () @end @implementation TableViewController - (id)initWithStyle

我有这个目标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
#导入
@接口TableViewController:UITableViewController
@结束
------
#导入“TableViewController.h”
@接口TableViewController()
@结束
@TableViewController的实现
-(id)initWithStyle:(UITableViewStyle)样式
{
self=[super initWithStyle:style];
如果(自我){
//自定义初始化
}
回归自我;
}
-(无效)viewDidLoad
{
[超级视图下载];
//取消对下一行的注释以保留演示文稿之间的选择。
//self.clearselectiononviewwillappear=否;
//取消对以下行的注释,以在此视图控制器的导航栏中显示编辑按钮。
//self.navigationItem.rightBarButtonItem=self.editButtonItem;
}
-(无效)视图卸载
{
[超级视频下载];
//释放主视图的所有保留子视图。
//例如,self.myOutlet=nil;
}
-(布尔)应自动旋转指针面定向:(UIInterfaceOrientation)interfaceOrientation
{
返回(interfaceOrientation==UIInterfaceOrientationGraphic);
}
#pragma标记-表视图数据源
-(NSInteger)表格视图中的节数:(UITableView*)表格视图
{
#警告:方法实现可能不完整。
//返回节数。
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
#警告:方法实现不完整。
//返回节中的行数。
返回50;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//配置单元格。。。
cell.textLabel.text=[NSString stringWithFormat:@“Zeile nummer%i”,indexath.row];
返回单元;
}
/*
//替代以支持表视图的条件编辑。
-(BOOL)tableView:(UITableView*)tableView caneditrowatinexpath:(nsindepath*)indepath
{
//如果不希望指定的项可编辑,则返回“否”。
返回YES;
}
*/
/*
//替代以支持编辑表格视图。
-(void)tableView:(UITableView*)tableView提交的编辑样式:(UITableViewCellEditingStyle)行的编辑样式索引路径:(NSIndexPath*)索引路径
{
如果(editingStyle==UITableViewCellEditingStyleDelete){
//从数据源中删除该行
[tableView deleteRowsAtIndexPaths:@[indexPath]和RowAnimation:UITableViewRowAnimationFade];
}   
else if(editingStyle==UITableViewCellEditingStyleInsert){
//创建相应类的新实例,将其插入数组,并向表视图添加新行
}   
}
*/
/*
//替代以支持重新排列表视图。
-(void)tableView:(UITableView*)tableView移动rowatinexpath:(nsindepath*)从indepath到indepath:(nsindepath*)到indepath
{
}
*/
/*
//重写以支持表视图的条件重新排列。
-(BOOL)tableView:(UITableView*)tableView可以移动rowatinexpath:(nsindepath*)indepath
{
//如果不希望该项目可重新订购,则返回“否”。
返回YES;
}
*/
#pragma标记-表视图委托
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath
{
//导航逻辑可能位于此处。创建并推送另一个视图控制器。
/*
*detailViewController=[[alloc]initWithNibName:@“bundle:nil];
// ...
//将选定对象传递给新的视图控制器。
[self.navigationController pushViewController:detailViewController动画:是];
*/
}
@结束
错误代码: 2012-09-16 21:05:36.204 renetest2[1176:fb03]*在-[UITableView\u createPreparedCellForGlobalRow:withIndexPath:],/SourceCache/UIKit\u Sim/UIKit-1914.84/UITableView.m:6061中断言失败 2012-09-16 21:05:36.206 renetest2[1176:fb03]*由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因是:“UITableView数据源必须从tableView返回一个单元格:cellForRowAtIndexPath:” ***第一次抛出调用堆栈: (0x14B402 0xeb4cd6 0x145ca48 0x9ae2cb 0xb1d28 0xb23ce 0x9dcbd 0xac6f1 0x55d21 0x14b5e42 0x1d85679 0x1d8f579 0x1d144f7 0x1d163f6 0x1DA316 0x15e84 0x16767 0x25183 0x25c38 0x19634 0x139eef5 0x1488195 0x13ecff2 0x13eb8da 0x13ead84 0x13eac9b 0x15c65 0x17626 0x1cad 0x1c15 0x1)
终止调用引发异常(lldb)

尝试理解错误并查看方法
tableView:cellforrowatinexpath
。您不应该配置单元格…而应该alloc/init您的单元格。

根据上面的示例,如果出列的单元格为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;
}
错误在这里:

UITableView数据源必须从tableView:cellforrowatinexpath:

这意味着

UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier]

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

您需要检查
单元格
是否为nil,然后创建一个新实例,如果它具有:

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

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

请解释您的问题,以及您为解决该问题所做的一切,并提出问题。不要只是简单地删除代码和崩溃日志。请从代码中删除所有不必要的行。下面是一个建议的提问格式:1)如何重现错误,2)您期望代码做什么(输出/行为),以及3)代码实际做什么。