2012-04-02 1 views
0

私が想像できる問題は非常に単純な問題です。コードからUITableViewControllerをロードしています

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 3; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"CityCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    cell.textLabel.text = @"Test"; 
    return cell; 
} 

そして、私は次のエラーを取得しています:

UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:' 
を私は3次のメソッド実装を持っている。このクラスでは

LocationViewController *lvc = [[LocationViewController alloc] init]; 
[self.navigationController pushViewController:lvc animated:true]; 

:私は1つUIViewControllerからLocationViewControllerと呼ばれるUITableViewControllerクラスをロードしています

StoryBoardを使用しているViewController間を移行するときに、前にこのエラーが発生しました。これは、CellIdentifierがcではなかったためですorrect。私は何が間違っているのか分かりません。このViewControllerでnibファイルを読み込もうとしましたが、同じエラーがスローされます。

+0

をこの** cellForRowAtIndexPathを:実際にあります呼び出されています(そして戻り値の前に 'cell'の値を記録するかもしれません)? –

答えて

3

セルを割り当てる必要があります。このコードを使用してください。

static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 
+0

それは完璧に答えた時間を割いていただきありがとうございます。 – SamRowley

0

使用、再利用可能なセルのキューにオブジェクトが存在しない場合、nilを返しdequeueReusableCellWithIdentifierこのコード

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
} 
cell.textLabel.text = @"Test"; 
return cell;} 
0

。 dequeueReusableCellWithIdentifierを呼び出した後にcellがnilかどうかを確認し、その場合は新しいUITableViewCellを作成します。

0

dequeueReusableCellWithIdentifierあなたは以下のコードを使用している場合は、それがキャッシュからセルを取得しようとする

あなたの識別子と、既に作成UITableViewCells「CityCell」のキャッシュであり、それは、それが1つを作成することができない場合や後で使用できるようにセルキャッシュに格納します。

スクロール時にテーブルのパフォーマンスが大幅に向上するため、大きなテーブルのキャッシュが必要です。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *MyIdentifier = @"CityCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease]; 
    } 
    cell.textLabel.text = @"Test"; 

    return cell; 
} 

これについての詳細を学ぶために、テーブルビューのプログラミングガイドを見てみましょう:**あなたがそれを確認しました...念のために

Table Programming Guide

関連する問題