2012-02-20 7 views
4

私のUITableViewのカスタムセルを作成しようとしています。私はXcode 4.2を使用していて、ARCとともにストーリーボードを使用しています。UITableViewカスタムセルスローSIGABRTエラー

私はそうのようなカスタムセルを表すクラスを作成しました:

ResultsCustomCell.h

#import <UIKit/UIKit.h> 

@interface ResultsCustomCell : UITableViewCell 
{ 
    IBOutlet UILabel *customLabel; 
} 

@property (nonatomic, retain) IBOutlet UILabel* customLabel; 

@end 

ResultsCustomCell.m

#import "ResultsCustomCell.h" 

@implementation ResultsCustomCell 

@synthesize customLabel; 

@end 

私はその後、実施していますUITableViewメソッドを次のように表示します。 ViewController.m

#import "ViewController.h" 
#import "ResultsCustomCell.h" 

@implementation ViewController 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
} 

// Set the number of items in the tableview to match the array count 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 5; 
} 

// Populate TableView cells with contents of array. 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"CellIdentifier"; 

    ResultsCustomCell *myCell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"]; 
    myCell.customLabel.text = @"helloWorld"; 

    return myCell; 
} 

// Define height for cell. 
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 80; 
} 

@end 

アプリケーションが正常にビルドが、その後すぐにクラッシュし、私に次のエラー与える:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:' 

は、私がどの部分を逃していますか?

+0

あなたはiOSの5のチェックも http://stackoverflow.com/questions/12016331/dequeuereusablecellwithidentifier-error-in-my-uitableview-in-ios5 – user1783937

答えて

0

私は自分の問題を解決するために何を行ったのか分かりませんが、正しく動作しています。ここで

は、私は現在、私のViewController.m

注意に持っているものです:ResultsCustomCell.hとの.mは上記と同じです。

#import "ViewController.h" 
#import "ResultsCustomCell.h" 

@implementation ViewController 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
} 

// Set the number of items in the tableview to match the array count 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 5; 
} 

// Populate TableView cells with contents of array. 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"CellIdentifier"; 

    // Make sure there are no quotations (") around the cell Identifier. 
    ResultsCustomCell *myCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    myCell.customLabel.text = @"helloWorld"; 

    return myCell; 
} 

// Define height for cell. 
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 80; 
} 

@end 

私はその後、確認しました正しい以下のもの:

  • テーブルビューセルが正しいカスタムクラスを持っている(ResultsCustomCell)。
  • UITableViewCellのセル識別子がInterface Builderで正しくありました。
  • インタフェースビルダーのUILabelがIBOutletに正しくリンクされていました。
  • のUITableViewのビューは、このコードを使用して、動作するように表示される上記の全てを確認した後、正しいコントローラ(のViewController)

を有していました。

1
@property (nonatomic, retain) IBOutlet UILabel* customLabel; 

自動参照カウント(ARC)は、retainの明示的な呼び出しを禁止します。これを削除してみてください。

エラーについては、カスタムセルではなくUITableViewCellを返します。さらに、ResultsCustomCellを割り当てないでください。

- (ResultsCustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"CellIdentifier"; 

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

    // Configure the cell. 
    cell.textLabel.text = @"Text"; 
    return cell; 
} 

さらに、UITableViewCellのサブクラスは、(当然の義務)メソッドinitを宣言していません。

ResultsCustomCell.h:

#import <UIKit/UIKit.h> 

@interface ResultsCustomCell : UITableViewCell 

@property (strong, nonatomic) UILabel *myLabel; 

@end 

ResultsCustomCell.m:

#import "ResultsCustomCell.h" 

@implementation ResultsCustomCell 

@synthesize myLabel; 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 

@end 

EDIT:私はあなたがストーリーボードを使用していることを、見ました。私の提案はあなたに役立つかもしれません。私はストーリーボードを一度も使用していない。

2

それは

// Populate TableView cells with contents of array. 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"CellIdentifier"; 

    ResultsCustomCell *myCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (myCell == nil) { 
     myCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    myCell.customLabel.text = @"helloWorld"; 

    return myCell; 
} 
+2

を使用している場合は、ストーリーボードとデキューをこれをしませんメソッドは、そこにセルがなければ、セルを作成して返します。 – jrturton

2

をデキューいないとき、あなたはあなたのプロトタイプ細胞に対するストーリーボードに再利用識別子(「CellIdentifier」)を追加するために忘れていたので、それを作成しようとしたときに新しいUITableViewCellを作成するのを忘れ新しいセル、ストーリーボードにその識別子のプロトタイプはなく、nilを返します。

+0

再利用識別子(CellIdentifier)を配置しましたが、まだこのエラーが発生しています。 – HGomez90

+0

cellForRowAtIndexPathメソッドにブレークポイントを設定します。呼び出されますか?あなたのテーブルビューコントローラのカスタムクラスを 'ViewController'に設定しましたか?もしそうでなければ、何もしないcellForRowの基本実装を呼び出すでしょう。 – jrturton

+0

私は正しくtableViewコントローラでカスタムクラスを設定しました。私もブレークポイントを配置し、cellForRowAtIndexPathが正しく呼び出されているようです。このメソッドはライン上で失敗します。 'myCell.customLabel.text = @" helloWorld ";' – HGomez90

0

私もあなたと同じ設定でこのエラーと戦っていました。私にとっては、iOS6の新しい「自動レイアウト」機能が原因であることに気付きました。それを無効にするには、カスタムセルxibを開き、右側のユーティリティで、ファイルインスペクタタブ(左端のタブ)を開きました。そこでは、私は "Use Autolayout"のチェックを外すことができました。

18

デバッガでは何が起こったのかを知ることができないので、これは問題です。例外が破棄され、「実行を続行」をクリックすると、単に停止します。

これは、私に問題を与えていたカスタムセルのUILabelを右クリックするまで、30分間に合わせました。答えを明らかにしました:あなたのセルのコントロールの1つが持っています(または、おそらく気づかずに)偽のコンセントを使用しています。 「スプリアスアウトレット」とは、あなたのクラスにもはや存在しないIBOutletプロパティに配線されているものを意味します。私の場合、私はプロパティの名前を変更し、それを再配線しましたが、古い参照コンセント接続を削除するのを忘れていました。

問題のあるコンセントを取り外すとすぐに問題は解決しました。

+0

シース。私はそれを信じられません。あなたの答えが現れてくれてありがとう。ありがとう! – averydev

+0

これは毎回私を得る! – SpaceTrucker

+0

非常に便利な答えです。予期しない! – sark9012

関連する問題