324

そこで、学校用の 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 Reader[4390:c07] * -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:] でのアサーションの失敗、/SourceCache/UIKit_Sim/UIKit-2372/UITableView.m:4460 2012-10-04 20: 13:05.357 Reader[4390:c07] *キャッチされていない例外 'NSInternalInconsistencyException' が原因でアプリを終了しています。 ' *First throw call stack: (0x1c91012 0x10cee7e 0x1c90e78 0xb64f35 0xc7d14 0x39ff 0xd0f4b 0xd101f 0xb980b 0xca19b 0x6692d 0x10e26b0 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: 例外をスローして呼び出された終了

エラー画面に表示されるコードは次のとおりです。

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

助けてください!

4

22 に答える 22

488

dequeueReusableCellWithIdentifier:forIndexPath:メソッドを使用しています。そのメソッドのドキュメントには、次のように書かれています。

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

再利用識別子の nib またはクラスを登録していません"Cell"

コードを見ると、nil提供するセルがない場合、 dequeue メソッドが戻ることを期待しているようです。dequeueReusableCellWithIdentifier:その動作には を使用する必要があります。

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

を使用する理由を理解したい場合はdequeueReusableCellWithIdentifier:forIndexPath:この Q&A を確認してください

于 2012-10-05T00:37:35.427 に答える
138

このエラーは、識別子の nib またはクラスの登録に関するものだと思います。

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

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

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

于 2012-12-13T09:42:13.443 に答える
69

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

したがって、CellIdentifier が「Cell」の場合は、「Identifier」プロパティを設定するだけです。 ここに画像の説明を入力

その後、必ずビルドをクリーンアップしてください。XCode では、ストーリーボードの更新で問題が発生することがあります

于 2013-02-18T15:36:17.543 に答える
59

私は同じ問題を抱えていました

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

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

    }

解決した

于 2012-10-26T06:24:31.930 に答える
19

Xcode 4.5にはデフォルトのテンプレート コードに新しい機能が含まれていることを付け加えておきます。これは、古い方法 dequeueReusableCellWithIdentifier:forIndexPath:
を期待している開発者にとって潜在的な落とし穴です。dequeueReusableCellWithIdentifier:

于 2012-10-12T19:25:48.383 に答える
18

ストーリーボードでは、プロトタイプ セルの「識別子」を CellReuseIdentifier「セル」と同じに設定する必要があります。そうすると、そのメッセージが表示されないか、その registerClass: 関数を呼び出す必要がなくなります。

于 2013-01-03T23:43:44.813 に答える
3

カスタム静的セルを使用する場合は、このメソッドにコメントしてください:

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

ストーリーボードの「属性インスペクター」でセルに識別子を付けます。

于 2013-02-05T04:52:06.537 に答える
3

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
}
于 2016-09-26T18:37:30.717 に答える
2

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

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

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

于 2013-10-23T20:03:05.090 に答える
2

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

h ファイルで委任を設定しました: @interface myViewController : UIViewController

私は自分の実装にデータソースコードを持っていましたが、それでもBOOM!、毎回クラッシュします! 「xxd」(番号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;
   }

ところで、ポップオーバーをボタンに固定する場合は、ボタンを IBAction および IBOutlet としてリンクする必要があります。

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

于 2013-02-17T09:54:23.390 に答える
2

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

static NSString *CellIdentifier = @"YourCellIdenifier";
于 2013-09-14T05:26:07.720 に答える
1

答えの単なる補足:すべてを正しく設定する場合があるかもしれませんが、次.xibのような他のUIを誤って追加する場合がありますUIButton:ここに画像の説明を入力 余分なUIを削除するだけで機能します.

于 2015-12-25T15:00:07.033 に答える
1

これは一部の人にとってはばかげているように見えるかもしれませんが、私はそれを理解しました. このエラーが発生しましたが、問題は静的セルを使用しようとしていたのに、動的に追加することでした。このメソッドを呼び出す場合、セルは動的プロトタイプである必要があります。ストーリーボードでセルを選択し、属性インスペクターの下で、最初に「コンテンツ」と表示され、静的ではなく動的プロトタイプを選択する必要があります。

于 2014-01-16T18:12:47.723 に答える
0

私の場合、電話をかけたときにクラッシュが発生しましたdeselectRowAtIndexPath:

ラインは[tableView deselectRowAtIndexPath:indexPath animated:YES];

FIXED MY PROBLEMに変更してください![self.tableView deselectRowAtIndexPath:indexPath animated:YES];

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

于 2016-03-02T22:34:29.077 に答える
0

Swift では、この問題は、次のコードを

viewDidLoad

方法。

tableView.registerClass(UITableViewCell.classForKeyedArchiver(), forCellReuseIdentifier: "your_reuse_identifier")
于 2016-06-19T05:37:02.220 に答える