web-dev-qa-db-ja.com

dequeueReusableCellWithIdentifier:forIndexPathでのアサーションエラー

だから私は私の学校のRSSリーダーを作っていて、コードを完成させました。私はテストを実行しました、そしてそれは私にその誤りを与えました。これが参照しているコードです。

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

        cell = 
         [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle  
                                reuseIdentifier:CellIdentifier];

    }

これが出力のエラーです。

2012-10-04 20:13:05.356リーダー[4390:c07]* [UITableView dequeueReusableCellWithIdentifier:forIndexPath:]、/SourceCache/UIKit_Sim/UIKit-2372/UITableView.mにアサーションエラーが発生しました:4460 2012-10-04 20:13:05.357リーダー[4390:c07] *キャッチされていない例外 'NSInternalInconsistencyException'のためアプリケーションを終了しています、理由: '識別子Cellを持つセルをデキューできません - 識別子のペン先またはクラスを登録するか、ストーリーボードにプロトタイプセルを接続する必要があります。 '*最初の呼び出しスタックを呼び出します。(0x1c91012 0x10cee7e 0x1c90e78 0xb64f35 0xc7d14 0x39ff 0xd0f4b 0xd101f 0xb980b 0xca92b 0x6692d 0x10e26b 0x228dfc0 0x228233c 0x228deaf 0x1058cd 0x4e1a6 0x4ccbf 0x4cbd9 0x4be34 0x4bc6e 0x4ca29 0x4f922 0xf9fec 0x46bc4 0x47311 0x2cf3 0x137b7 0x13da7 0x14fab 0x26315 0x2724b 0x18cf8 0x1becdf9 0x1becad0 0x1c06bf5 0x1c06962 0x1c37bb6 0x1c36f44 0x1c36e1b 0x147da 0x1665c 0x2a02 0x2935)のlibc ++ abi.dylib:例外をスロー呼ばTERMINATE

これがエラー画面に表示されるコードです。

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

助けてください!

317
Nick Scottman

あなたはdequeueReusableCellWithIdentifier:forIndexPath:メソッドを使っています。そのメソッドの ドキュメント はこう言っています:

重要:このメソッドを呼び出す前に、クラスまたはnibファイルをregisterNib:forCellReuseIdentifier:またはregisterClass:forCellReuseIdentifier:メソッドを使用して登録する必要があります。

再利用識別子"Cell"のペン先またはクラスを登録しませんでした。

あなたのコードを見ると、あなたがそれを与えるためのセルを持っていなければ、dequeueメソッドがnilを返すことを期待しているようです。そのためにはdequeueReusableCellWithIdentifier:を使う必要があります。

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

dequeueReusableCellWithIdentifier:dequeueReusableCellWithIdentifier:forIndexPath:は異なるメソッドであることに注意してください。 前者 および 後者 についてはdocを参照してください。

なぜdequeueReusableCellWithIdentifier:forIndexPath:を使いたいのかを理解したいのなら、 このQ&Aをチェックしてください

483
rob mayoff

私は、このエラーはあなたのペン先またはクラスを識別子に登録することにあると思います。

TableView:cellForRowAtIndexPath関数で自分がしていることを維持し、viewDidLoadに以下のコードを追加するだけです。

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];

それは私のために働いた。それが役立つかもしれないことを願っています。

137
user843910

この質問はかなり古いですが、別の可能性もあります。ストーリーボードを使用している場合は、ストーリーボードでCellIdentifierを設定するだけです。

あなたのCellIdentifierが "Cell"の場合は、 "Identifier"プロパティを設定するだけです。 enter image description here

そうした後であなたのビルドをきれいにするようにしてください。 XCodeはストーリーボードのアップデートで問題を起こすことがあります

67

私はに置き換えて同じ問題を抱えていた

static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

    }

解決した

59
iMeMyself

この問題は、ストーリーボードでカスタムのUITableViewCellを設定していても、このUITableViewControllerを使用するUITableViewCellをインスタンス化するためにstoryboardを使用していないために発生する可能性があります。たとえば、MainStoryboardには、UITableViewControllerというMyTableViewControllerサブクラスがあり、UITableViewCellというカスタム動的MyTableViewCellがあり、識別子IDは "MyCell"です。

あなたがこのようにあなたのカスタムUITableViewControllerを作成するならば:

 MyTableViewController *myTableViewController = [[MyTableViewController alloc] init];

カスタムtableviewcellが自動的に登録されることはありません。手動で登録する必要があります。

しかし、ストーリーボードを使用してMyTableViewControllerをインスタンス化すると、次のようになります。

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
MyTableViewController *myTableViewController = [storyboard  instantiateViewControllerWithIdentifier:@"MyTableViewController"];

いいことが起こります! UITableViewControllerは自動的にあなたがストーリーボードにあなたが定義したあなたのカスタムTableviewセルを登録します。

デリゲートメソッド "cellForRowAtIndexPath"では、このようにテーブルビューセルを作成できます。

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

//Configure your cell here ...

return cell;
}

リサイクルキューに再利用可能なセルがない場合、dequeueReusableCellWithIdentifierは自動的に新しいセルを作成します。

これで完了です。

25
James Wang

Xcode 4.5に新しいdequeueReusableCellWithIdentifier:forIndexPath:が追加されました。
デフォルトのテンプレートコードに - 古いdequeueReusableCellWithIdentifier:メソッドを期待する開発者にとっての潜在的な問題。

19
Brian Shriver

Swift 2.0ソリューション:

あなたはあなたの属性インスペクタに入り、あなたのセルの名前を追加する必要があります。

enter image description here

次に、あなたの識別子をあなたのデキューと一致させる必要があります。

let cell2 = tableView.dequeueReusableCellWithIdentifier("ButtonCell", forIndexPath: indexPath) as! ButtonCell

あるいは

ペン先で作業している場合は、cellForRowAtIndexPathにクラスを登録する必要があります。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {       

    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "SwitchCell")

    // included for context            
    let cell = tableView.dequeueReusableCellWithIdentifier("SwitchCell", forIndexPath:indexPath) as! SwitchCell

    //... continue    
}

AppleのUITableViewクラスリファレンスには、次のように記載されています。

セルをデキューする前に、このメソッドまたはregisterNib:forCellReuseIdentifier:メソッドを呼び出して、テーブルビューに新しいセルの作成方法を指示します。指定されたタイプのセルが現在再利用キューに入っていない場合、テーブルビューは提供された情報を使用して新しいセルオブジェクトを自動的に作成します。

以前に同じ再利用識別子でクラスまたはnibファイルを登録した場合は、cellClassパラメータで指定したクラスが古いエントリを置き換えます。指定した再利用識別子からクラスを登録解除する場合は、cellClassにnilを指定します。

これがApples Swift 2.0フレームワークのコードです。

// Beginning in iOS 6, clients can register a nib or class for each cell.
// If all reuse identifiers are registered, use the newer -dequeueReusableCellWithIdentifier:forIndexPath: to guarantee that a cell instance is returned.
// Instances returned from the new dequeue method will also be properly sized when they are returned.

@available(iOS 5.0, *)
func registerNib(nib: UINib?, forCellReuseIdentifier identifier: String)

@available(iOS 6.0, *)
func registerClass(cellClass: AnyClass?, forCellReuseIdentifier identifier: String)
18
Dan Beaulieu

ストーリーボードでは、プロトタイプセルの 'Identifier'をCellReuseIdentifier "Cell"と同じに設定する必要があります。そうすれば、そのメッセージを受け取ることも、registerClass:関数を呼び出す必要もなくなります。

18
Steven

Swift 3.0を使った作業:

override func viewDidLoad() {
super.viewDidLoad()

self.myList.register(UINib(nibName: "MyTableViewCell", bundle: nil), forCellReuseIdentifier: "Cell")
}



public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = myList.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath) as! MyTableViewCell

return cell
}
3
norbDEV

カスタムスタティックセルを使用する場合は、このメソッドをコメントアウトしてください。

//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    static NSString *CellIdentifier = @"notificationCell";
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//    return cell;
//}

そして、ストーリーボードの "Attributes Inspector"でセルに識別子を与えます。

Objective CとSwiftの両方で答えを教えてください。

dequeueReusableCellWithIdentifier:forIndexPath:を使用する場合、このメソッドを Apple Documnetation Says のように呼び出す前に、registerNib:forCellReuseIdentifier:またはregisterClass:forCellReuseIdentifier:メソッドを使用してクラスまたはnibファイルを登録する必要があります。

だから私たちはregisterNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier:を追加します

指定した識別子のクラスを登録して新しいセルを作成する必要がある場合、このメソッドはinitWithStyle:reuseIdentifier:メソッドを呼び出してセルを初期化します。 nibベースのセルの場合、このメソッドは提供されたnibファイルからセルオブジェクトをロードします。既存のセルが再利用可能な場合、このメソッドは代わりにセルのprepareForReuseメソッドを呼び出します。

viewDidLoadメソッドでセルを登録する必要があります

目標C

オプション1:

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

オプション2:

[self.tableView registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellReuseIdentifier:@"cell"];

上記のコードでnibWithNibName:@"CustomCell"は私のペン先名CustomCellの代わりにあなたのペン先名を与えます

スイフト

オプション1:

tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

オプション2:

tableView.registerNib(UINib(nibName: "NameInput", bundle: nil), forCellReuseIdentifier: "Cell") 

上記のコードではnibName:"NameInput"あなたのペン先の名前を与える

3
user3182143

ストーリーボードですべてを正しく設定してクリーンビルドを実行しましたが、「識別子にペン先またはクラスを登録するか、ストーリーボードにプロトタイプセルを接続する必要があります」というエラーが発生し続けました

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];

エラーを修正しましたが、まだ迷っています。私は「カスタムセル」を使用していません。テーブルビューを埋め込んだビューだけです。ビューコントローラをデリゲートおよびデータソースとして宣言し、セル識別子がファイル内で一致することを確認しました。何が起きてる?

2
FrostyL

確かに、ストーリーボードでセル識別子を設定するのを忘れたときにも、この同じエラーが発生しました。これがあなたの問題であれば、ストーリーボードのテーブルビューのセルをクリックし、属性エディタでセル識別子を設定してください。ここで設定したセル識別子が次のものと同じであることを確認してください。

static NSString *CellIdentifier = @"YourCellIdenifier";
2
honkskillet

私は同じ問題を抱えていた、同じエラーを抱えていたと私にとってそれはこのように働いた:

[self.tableView registerNib:[UINib nibWithNibName:CELL_NIB_HERE bundle: nil] forCellReuseIdentifier:CELL_IDENTIFIER_HERE];

たぶんそれは他の誰かに役立つでしょう。

2
CarmenA

昨夜、プログラムで生成されたテーブルが[myTable setDataSource:self]でクラッシュした理由を解明するのに何時間も費やしました。空のテーブルをコメントアウトしてポップアップするのは問題ありませんでしたが、データソースにアクセスしようとするたびにクラッシュしました。

Hファイルに委任を設定しました。@interface myViewController:UIViewController

私は自分の実装にデータソースコードを持っていて、それでもBOOM!、毎回クラッシュします! "xxd"(nr 9)に感謝します。そのコード行を追加して解決しました!実際、私はIBActionボタンからテーブルを起動しているので、ここに私の完全なコードがあります。

    - (IBAction)tapButton:(id)sender {

    UIViewController* popoverContent = [[UIViewController alloc]init];

    UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 300)];
    popoverView.backgroundColor = [UIColor greenColor];
    popoverContent.view = popoverView;

    //Add the table
    UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 200, 300)         style:UITableViewStylePlain];

   // NEXT THE LINE THAT SAVED MY SANITY Without it the program built OK, but crashed when      tapping the button!

    [table registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    table.delegate=self;
    [table setDataSource:self];
    [popoverView addSubview:table];
    popoverContent.contentSizeForViewInPopover =
    CGSizeMake(200, 300);

    //create a popover controller
    popoverController3 = [[UIPopoverController alloc]
                          initWithContentViewController:popoverContent];
    CGRect popRect = CGRectMake(self.tapButton.frame.Origin.x,
                                self.tapButton.frame.Origin.y,
                                self.tapButton.frame.size.width,
                                self.tapButton.frame.size.height);


    [popoverController3 presentPopoverFromRect:popRect inView:self.view   permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];



   }


   #Table view data source in same m file

   - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
   {
    NSLog(@"Sections in table");
    // Return the number of sections.
    return 1;
   }

   - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
  {
    NSLog(@"Rows in table");

    // Return the number of rows in the section.
    return myArray.count;
   }

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

    NSString *myValue;

    //This is just some test array I created:
    myValue=[myArray objectAtIndex:indexPath.row];

    cell.textLabel.text=myValue;
    UIFont *myFont = [ UIFont fontWithName: @"Arial" size: 12.0 ];
    cell.textLabel.font  = myFont;

    return cell;
   }

ちなみに:Popoverをそれに固定したい場合は、ボタンをIBActionとして、およびIBOutletとしてリンクする必要があります。

UIPopoverController * popoverController3はHファイル内で@interface {}の直後に宣言されています

2
Simone

答えを補足するだけです。すべてを正しく設定した時間があるかもしれませんが、UIButtonのように、誤って他のUIを.xibに追加することがありますenter image description here 余分なUIを削除するだけで、うまくいきます。

1
DrustZ

CellIdentifier ==ストーリーボード内のセルの識別子、両方の名前が同じであることを確認してください。これがあなたのために働くことを願っています

1
Ankit garg

これは何人かの人々にばかげて見えるかもしれませんが、それは私を得ました。私はこのエラーに遭遇していて、私にとっての問題は、私が静的セルを使用しようとしていましたが、それからもっと多くのものを動的に追加することでした。このメソッドを呼んでいるのであれば、セルは動的プロトタイプである必要があります。ストーリーボードの「属性」インスペクタの下にあるセルを選択します。最初のものは「コンテンツ」です。静的ではなく動的なプロトタイプを選択する必要があります。

1
Chase Roberts

Swiftでは、この問題は以下のコードを追加することで解決できます。

viewDidLoad

方法。

tableView.registerClass(UITableViewCell.classForKeyedArchiver(), forCellReuseIdentifier: "your_reuse_identifier")
0
arango_86

私の場合、deselectRowAtIndexPath:を呼び出したときにクラッシュが発生しました

行は[tableView deselectRowAtIndexPath:indexPath animated:YES];でした

それを[self.tableView deselectRowAtIndexPath:indexPath animated:YES];に変更します私の問題を修正しました!

これが誰にでも役立つことを願っています

0
Offek