2012-09-16 56 views
-4

我有這個目標的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 

錯誤碼: 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 dataSource必須從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) 終止叫做拋出異常(LLDB)

+2

請解釋您的問題,無論您嘗試解決問題,並提出問題。不要簡單地記下你的代碼和崩潰日誌。 –

+1

請從您的代碼中刪除所有不必要的行。這裏有一個提出問題的建議格式:1)你如何重現錯誤,2)你期望代碼做什麼(輸出/行爲),3)代碼實際上做了什麼。 – BryanH

回答

0

從上面的示例中,如果出隊單元爲零,則不會創建單元。該功能的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; 
} 
0

試着去了解錯誤,並看看方法tableView:cellForRowAtIndexPath。而不是// Configure the cell...你應該分配/初始化你的手機。

0

的錯誤是在這裏:

UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:

這意味着該行

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

一直沒有能出隊一個單元實例,所以該方法返回nil

您需要檢查cell是零,然後創建一個新的實例,如果它是:

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

然後您就可以配置新的UITableViewCell實例。