2017-03-11 8 views
2

.xibファイルを使用してテーブルセルを作成しています。エラーのある.xibセルを持つUITableView:データソースからセルを取得できませんでした

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    DesplayCell *cell = [tableView dequeueReusableCellWithIdentifier:@"desplayCell"]; 

    if (!cell) { 
     [tableView registerNib:[UINib nibWithNibName:@"DisplayCell" bundle:nil] forCellReuseIdentifier:@"desplayCell"]; 
    } 

    // 
return cell; 
} 

アプリケーションがエラーでクラッシュ:

は、私はあなたのコードでdequeueReusableCellWithIdentifierを追加することを忘れ、そのデータソース

+0

ストーリーボードまたはXIBファイル内のセルが同じ識別子 'desplayCellを'持っていることを確認します。 –

+0

どちらも同じです。 –

答えて

1

からセルを得ることができなかった。そして次のコードを使用してのUITableViewに追加

dequeueReusableCellWithIdentifier使い方

あなたは、同じ形式のすべてのセルに同じ再利用識別子を使用する必要があります。 再利用識別子は、セルの内容を引いたものと同じ一般的な構成を持つテーブルビューのセル(行)に関連付けられています。

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    DesplayCell *cell = [tableView dequeueReusableCellWithIdentifier:@"desplayCell"]; 

    if (!cell) { 
     [tableView registerNib:[UINib nibWithNibName:@"DisplayCell" bundle:nil] forCellReuseIdentifier:@"desplayCell"]; 
     cell = [tableView dequeueReusableCellWithIdentifier:@"desplayCell"]; 
    } 

    // 
return cell; 

}

+0

"viewdidload"にセルを登録し、再利用可能なセルdequeueReusableCellWithIdentifier:(NSString *)識別子 forIndexPath:(NSIndexPath *)indexPathを作成する必要があります。次の関数は、指定された再利用識別子の再利用可能なテーブルビューセルオブジェクトを返し、それをテーブルに追加するためです。 – Pawan

0

あなたは下に書くことができるコードcellForRowAtIndexPathで

[tableView registerNib:[UINib nibWithNibName:@"DisplayCell" bundle:nil] forCellReuseIdentifier:@"desplayCell"]; 

下に書くのviewDidLoadでこの

のように使用することができます

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     DesplayCell *cell = [tableView dequeueReusableCellWithIdentifier:@"desplayCell"]; 

      if (cell == nil) { // if cell is nil then you can alloc it 

     cell=[[DesplayCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"desplayCell"]; 

      cell = [tableView dequeueReusableCellWithIdentifier:@"desplayCell"]; 

     } 

return cell; 

} 
0

答えはSiddhesh Mhatrepostですが、UITableViewのカテゴリを作成する必要があります。これは大きなプロジェクトで便利です。

のUITableView + Category.h

- (id)dequeueReusableOrRegisterCellWithIdentifier:(NSString *)identifier; 
- (id)dequeueReusableOrRegisterCellWithClass:(Class)aClass; 

のUITableView + Category.m

- (UITableViewCell *)dequeueReusableOrRegisterCellWithIdentifier:(NSString *)identifier{ 
    UITableViewCell *cell = [self dequeueReusableCellWithIdentifier:identifier]; 

    if (!cell) { 
     [self registerNib:[UINib nibWithNibName:identifier bundle:nil] forCellReuseIdentifier:identifier]; 
     cell = [self dequeueReusableCellWithIdentifier:identifier]; 
    } 

    return cell; 
} 

- (UITableViewCell *)dequeueReusableOrRegisterCellWithClass:(Class)aClass{ 
    UITableViewCell *cell = [self dequeueReusableCellWithIdentifier:NSStringFromClass(aClass)]; 

    if (!cell) { 
     [self registerClass:aClass forCellReuseIdentifier:NSStringFromClass(aClass)]; 
     cell = [self dequeueReusableCellWithIdentifier:NSStringFromClass(aClass)]; 
    } 

    return cell; 
} 
関連する問題