2012-04-30 6 views
0

最初のボタンは大統領のリストを表示し、それらをクリックするとウィキペディアのページが表示されます。私は別のボタンを押して、ページの言語を英語からドイツ語などに変更できるようにしました。私はデバッガでこのエラーが発生し続けています:キャッチされていない例外 'NSInternalInconsistencyException'のためアプリを終了し続けるiPadアプリを持っています

アサーションエラー - [UITableView _createPreparedCellForGlobalRow:withIndexPath:]、 /SourceCache/UIKit_Sim/UIKit-1914.84/UITableView.m:6061 2012-04-30 17:30:01.293会長[11151:f803] *キャッチされていない例外 'NSInternalInconsistencyException'のためアプリを終了しています、理由: 'UITableView dataSourceが返されます。 tableViewからセル:cellForRowAtIndexPath:」* は、まずスローコールスタック:

私は本のチュートリアルをつもりだと私はもう一度、彼らはコードが欠落していると思います。どんなアイデアが素晴らしいだろう!

EDIT:Noob私は間違ったコードセクションを貼り付けてみました。この新しいセクションは、正常に動作していたBIDMasterViewControllerの後に追加したカスタムポップオーバーです。問題が到着したところ、この新しいセクションでは、次のとおりです。

#import "BIDLanguageListController.h" 
#import "BIDDetailViewController.h" 

@interface BIDLanguageListController() 

@end 

@implementation BIDLanguageListController 

@synthesize languageNames; 
@synthesize languageCodes; 
@synthesize detailViewController; 

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

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.languageNames = [NSArray arrayWithObjects:@"English", @"French", 
          @"German", @"Spanish", nil]; 
    self.languageCodes = [NSArray arrayWithObjects:@"en", @"fr", @"de", @"es", nil]; 
    self.clearsSelectionOnViewWillAppear = NO; 
    self.contentSizeForViewInPopover = CGSizeMake(320.0, [self.languageCodes count] * 44.0); 

    // 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]; 

    self.detailViewController = nil; 
    self.languageNames = nil; 
    self.languageCodes = nil; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

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

#pragma mark - Table view data source 

- (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 [self.languageCodes count]; 
} 

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

    // Configure the cell... 
    cell.textLabel.text = [languageNames objectAtIndex:[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:[NSArray arrayWithObject: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 
{ 
    detailViewController.languageString = [self.languageCodes objectAtIndex: 
              [indexPath row]]; 
} 

@end 
+0

これは明らかにランタイムエラーであるため、 'compiler-error'タグを削除しました... – Macmade

+0

あなたはUITableViewCellを返すようです...あなたのセルがnilである可能性がありますか? – CodaFi

+0

[UITableView dataSourceの可能な複製は、tableViewからセルを返す必要があります:cellForRowAtIndexPath:Exception](http://stackoverflow.com/questions/2224316/uitableview-datasource-must-return-a-cell-from-tableviewcellforrowatindexpath) – CodaFi

答えて

1

実際にセルをインスタンス化することはありません。 UITableViewCellを再利用する方法については、Web上で多くの優れたドキュメントがあります。

Appleのテーブルビューのプログラミングガイドは素晴らしいですし、間違いなく読まなければなりません: 一方http://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7-SW1

は、簡単な解決策として、のようなものにあなたの方法を変更します着目

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

    //THIS IS THE IMPORTANT PART YOU ARE MISSING 
    if(cell == nil) { 
     //AND IF YOU PUT AN NSLOG IN HERE, YOU'LL SEE THIS IS CALLED ABOUT THE 
     //NUMBER OF TIMES FOR THE INITIALLY VISIBLE CELLS ON THE SCREEN, PLUS 1 OR 2 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell... 
    cell.textLabel.text = [languageNames objectAtIndex:[indexPath row]]; 

    return cell; 
} 
+0

右上に!完璧に働いた....私はなぜこの本がそのような小さくて重要なコード部分を省略するのか分からない。私はちょうど始まったばかりで、これらのチュートリアルにエラーがあると迷惑になります。このサイトに感謝し、それは役に立つ人々です。再度、感謝します! – Devj

0

あなたは1つがあなたのUITableViewからデキューすることができない場合のUITableViewCellを開始する必要があります。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell... 
    cell.textLabel.text = [languageNames objectAtIndex:[indexPath row]]; 

    return cell; 
} 
+0

あなたの迅速で正確な応答もありがとう! – Devj

0

のXcodeのテンプレートいますチュートリアルと同じミスを犯したので、もう少し調べてみると、別の結論に達しました。この問題は、セルのインスタンス化ではなく、cellIdentifierでの問題です。この問題は、クラス内の.xib またはのいずれかでcellIdentifierを変更したときに発生しますが、両方では変更されません。 .xibが最初に読み込まれると、明らかにセルが.xibで指定された識別子でキューに入れられます。コードが実行されるとき、コードが@ "Cell"のような別のcellIdentifierを主張しない限り、セルはいつでもデキューできます。セルがゼロであるかどうかをチェックし、セルをインスタンス化することは、再利用可能なセルの目的をむしろ無効にします。 私の答えは、すべてのif(cell == nil)ステートメントを取り除き、代わりにセルの識別子を確認することです。

+0

私はストーリーボードを使用しています。私は両者でそれをしたことを信じています。自分のセル識別子を確認するにはどうすればいいですか? – Devj

+0

Interface Builderでセルを選択し、Attributes Inspectorのidentifierフィールドの値を確認します。これはコードで指定された値と同じですか、つまり静的NSString * CellIdentifier = @ "Cell"です。 ? –

関連する問題