• ベストアンサー
※ ChatGPTを利用し、要約された質問です(原文:iphoneアプリ開発についての質問です)

UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath: エラーの原因と解決方法

Lchan0211bの回答

  • ベストアンサー
回答No.2

質問を読んで気になった部分をコメントします。 > static const id identifiers = { @"MetaCell", @"StatusCell", @"EvoCell", @"FormCell" }; これ、本当にこう書いてますか? これだとwarningが表示され、このまま実行させると、別の理由で例外発生になると思うのですが。 正しくは static const id identifiers[] = { @"MetaCell", @"StatusCell", @"EvoCell", @"FormCell" }; じゃないですか? というか、問題が発生しているコードをそのままコピペして質問していますか? 適当に編集して質問したら、問題ないところが指摘されたり、問題となる原因が 消えたりするかもしれません。 > UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; iOS6以降がターゲットなら [tableView dequeueReusableCellWithIdentifier:CellIdentifier] じゃなくて [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath] とすべきだと思います。 (参考) http://dev.classmethod.jp/smartphone/iphone/ios6-uitableview/ ただ、おそらくここを変えても結局identifierで設定したIDを見つけられなくて 別のエラーメッセージで失敗になりそうに思います。 > identifierの文字列は何度見ても一致していました。 ・大文字/小文字は一致しているか? ・全角の英数字を使用していないか? ・後ろにスペースがついてたりしないか? こういった点も大丈夫ですか? 念のためコーディングに記載したidentifierをコピペして 設定し直した方がよいかもしれません。 あと、identifierを設定したセルは、今回処理するテーブル内に 定義されているのですよね? もし複数テーブルを表示してたりしたら、どっちのテーブルの処理か 切り分けて処理する必要があるかもしれません。

関連するQ&A

  • iphoneアプリ開発についての質問です

    http://okwave.jp/qa/q8846564.htmlの質問に補足を付け加えたものです。 storyboardのprototype cellsでカスタムセルを4つ作成しidentifierも設定した状態で、 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static const id identifiers = { @"MetaCell", @"StatusCell", @"EvoCell", @"FormCell" }; NSString *CellIdentifier = identifiers[indexPath.section]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 以下略 という風にしているのですが UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath: というエラーが出てしまいます。 identifierの文字列は何度見ても一致していました。 if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 上記のコードを追加しても、一応エラーは出ませんが当然セルは反映せず真っ白のままです。 if (cell == nil) { if (indexPath.section == 0) { cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; } else if (indexPath.section == 1) { cell = [tableView dequeueReusableCellWithIdentifier:@"StatusCell"]; } else if (indexPath.section == 2) { cell = [tableView dequeueReusableCellWithIdentifier:@"EvoCell"]; } else if (indexPath.section == 3) { cell = [tableView dequeueReusableCellWithIdentifier:@"FormCell"]; } } 一応このようなやりかたでも試しましたが結果は同じでした。 ちなみにこのテーブルビューへは前のビューからsegueで (void)[[segue destinationViewController] initDetailDic:dic]; で遷移してきています。 何度調べても原因がわからず困っています。心当たりはないでしょうか。 *補足 問題の起こっているこのテーブルビューへは、前のテーブルビューから遷移してきているのですが試しにNavigationControllerのrootViewに設定してみたところちゃんと表示されました。画面遷移の方法に原因があると考えられるのですが、上に書いたやり方はごくごく一般の方法だとおもうのですが・・・

  • iphoneアプリ開発での変数の受け渡し

    最近iPhoneアプリ開発を始めた者です。 変数の受け渡しについて質問があり、投稿させていただきました。 以下のコード内で、 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 以下で設定したIDに入ったデータを、 -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)senderの中のIDに受け渡したいのですが、どうやら2つのIDが繋がっていません。 自分自身あまりまだ理解できていないこともあり、ざっくりとした聞き方で申し訳ありません。 初歩的な質問だとは思うのですが、なにとぞご教授お願いします。 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 2; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(cell == nil){ cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } if (indexPath.row == 0){ cell.textLabel.text = @"東京"; }else{ cell.textLabel.text = @"大阪"; } int ID = indexPath.row; return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self performSegueWithIdentifier:@"mySegue" sender:self]; } -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"mySegue"]) { SecondTableViewController *viewCon = segue.destinationViewController; viewCon.myValue = ID; } }

  • iphoneアプリ開発についての質問です

    tableviewcellにチェックマークを付けるだけの簡単なコードですが、なぜかチェックマークを付けたセルとは別のセルにまでチェックマークがついてしまっています。 また、激しくスクロールするとさっきまで付いていたチェックマークが外れていたりとさらに酷い状態に・・・。 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if (cell.accessoryType == UITableViewCellAccessoryCheckmark) cell.accessoryType = UITableViewCellAccessoryNone; else cell.accessoryType = UITableViewCellAccessoryCheckmark; [tableView deselectRowAtIndexPath:indexPath animated:YES]; } いろんなサイトで見ても上記のコードが最もよく見るコードでした。 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *CellIdentifier = @"ButtonCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; [self updateCell:cell :CellIdentifier atIndexPath:indexPath]; return cell; } - (void)updateCell:(UITableViewCell *)cell :(NSString *)CellIdentifier atIndexPath:(NSIndexPath *)indexPath { BButton *type = (BButton *)[cell viewWithTag:1]; //Buttonはライブラリを使用 [ReturnButton typeButton:type :self.showArr[indexPath.row]]; } チェックマーク以外はUIButtonなども正常に表示されました。 心当たりがあればお願いします。

  • iphoneアプリ 画像反転の方法

    もしとくに反転のコード組まなければ、実機上でtableviewのcellをクリックすると、反転して青くなって、画面が遷移されます。 考えているのは、回転ではなく反転です。 今回やりたいのは、tableviewのcell上にグレーの画像を貼っておいて、それをクリックすると黒の画像に反転する方法です。 gray.pngを貼りつけてグレーの画面にはできているのですが、 1、黒の画像(black.png)をどうやって貼るのか 2、cellをクリックしたときにどうやってグレー画像から黒画像に反転するのか の2点を教えて頂けたら幸いです。よろしくお願いします。 下記、コードになります。 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return 2; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } if ([indexPath row]==0) { UIImage *image = [UIImage imageNamed:@"gray1.png"]; cell.imageView.image = image; }else if ([indexPath row]==1) { UIImage *image = [UIImage imageNamed:@"gray2.png"]; cell.imageView.image = image; } return cell; }

    • ベストアンサー
    • Mac
  • iphoneアプリ開発についての質問です

    XCodeでUISearchBarをUITableViewヘッダーにおいてテーブルビューの要素を検索したいのですがエラーになります。 サイズがテキスト量によって可変するカスタムセルをStoryboardで作れないのでコードで作成したんですが、その場合だとサーチバーにテキストを入力し検索を押した時点でエラーになり *** Assertion failure in -[UISearchResultsTableView dequeueReusableCellWithIdentifier:forIndexPath:], となってしまいます。 セル生成のコードは以下になります - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; //ココでエラー NSDictionary *dic; if (tableView == self.searchDisplayController.searchResultsTableView) { dic = self.searchResult[indexPath.row]; } else { dic = self.itemArr[indexPath.row]; } cell.title.text = dic[@"title"]; cell.label.text = dic[@"detail"]; CGSize textSize = [dic[@"detail"] sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:CGSizeMake(300, 200) lineBreakMode:NSLineBreakByWordWrapping]; cell.label.frame = CGRectMake(10, 35, textSize.width, textSize.height); return cell; } Storyboard上のprototypecellなら問題なく動作するのですが、どうしてもコード生成のカスタムセルだとエラーになってしまうようです。 アドバイスお願いします。

  • iphoneプログラミング UITableの複数表示

    すいません、基本的なことだとは思うのですが、どうしても分からないため質問させてください。 UITableView を2つ並べてそれぞれに別々の値を入れていきたいのですがどうすればできますか?? - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *MyIdentifier = @"MyIdentifer"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; } cell.text = [listOfContents objectAtIndex:indexPath.row]; return cell; } この関数だとUITableの個別の識別ができないため2つとも同じ値が入ってしまいます。

  • iPhoneアプリ 画面遷移での変数受け渡し

    現在、iPhoneアプリの開発を勉強している者です。 少し詰まってしまった箇所があるので、ご教授いただけると幸いです。 TableViewController間での変数の受け渡しを実装しており、1枚目のTableViewでタップされたセルのテキストを2枚目に渡す、というコードを書きました。 受け渡しに関するコードは以下の部分です。 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self performSegueWithIdentifier:@"mySegue" sender:self]; } -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString:@"mySegue"]) { SecondTableViewController *viewCon = segue.destinationViewController; NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; viewCon.myValue = cell.textLabel.text; } } これ自体は問題なく動いてくれたのですが、SecondTableViewControllerにNavigationControllerをEmbed Inしたところ、1枚目でセルをタップした際にエラーが出てアプリが止まってしまうようになりました。 BreakPointを設けてみると上記コードのviewCon.myValue = cell.textLabel.text;のところで止まっており、unrecognized selector sent to instanceというエラーが吐き出されていました。 同様のエラーが出た例などを調べてみたのですが自力では解決できず・・・、どなたか原因を指摘して頂けるとありがたいです。 よろしくお願いします。

  • TableViewでセル名が表示されない

    こんにちは。 iPhoneアプリの勉強している者ですが、TableViewControllerを使いセルごとに名前を表示させようとしているのですが、タイトルのみが表示されて肝心のセル名が表示されません。 appDelegate.mにてUINavigationControllerのインスタンス及びUITableViewControllerのインスタンスを作成してUITableViewControllerのインスタンスをrootViewControllerに設定しTableViewを表示させようとしています。 プログラムの記述のどこが間違っているのか教えて頂けると大変助かります。 以下UITableViewControllerのinterfaceセクションとimplementationセクションを掲載しますので、ご指摘お願いします。 #import <UIKit/UIKit.h> @interface SampleForSimpleTable : UITableViewController { NSArray *itemes; } @end @implementation SampleForSimpleTable - (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.title = @"SimpleTable"; itemes = [[NSArray alloc] initWithObjects:@"ITME 1", @"ITEM 2", @"ITEM 3", @"ITEM 4", @"ITEM 5", @"ITEM 6", nil]; } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { #warning Potentially incomplete method implementation. // Return the number of sections. return 0; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { #warning Incomplete method implementation. // Return the number of rows in the section. return [itemes count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier = @"default-cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier ]; if (!cell){ cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } cell.textLabel.text = [itemes objectAtIndex:indexPath.row]; // Configure the cell... return cell; }

  • UITableViewCellのカスタマイズ

    ObjectiveCを用いてiPhoneアプリ開発の勉強をしています。 ■UITableViewCellに、様々なコントロール要素をのせて表示する方法 http://www.yoheim.net/blog.php?q=20130106 このサイトを参考に、UITableViewCellに、セルごとにボタンやラベルをなどのレイアウトを設定して表示するように取り組んでいるのですが、うまくいきません。 サイトの解説では1つ1つのセルを作る解説がされていて、1つのセルをレイアウトして表示することはできるのですが、 トップ画像のように様々なレイアウトのセルを合わせて表示する方法が解説されておらず、どうすればいいのか分かりませんでした。 TableView部分のコードは以下のように記述しています。 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 2; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // tableCellを取得する。 static NSString *identifier = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } UISwitch *sw = [[UISwitch alloc] initWithFrame:CGRectZero]; // UISwitchはUIContolを継承しているので、タップされた際の動作を簡単に指定できる。 [sw addTarget:self action:@selector(tapSwich:) forControlEvents:UIControlEventTouchUpInside]; // accessoryViewに代入するときれいに動作します。 cell.accessoryView = sw; cell.textLabel.text = @"Flash Light"; return cell; } この通りに書いてシミュレータを起動すると、サイトトップ画像の一番上のセルと同じレイアウトのセルが2つ表示されます。 (Flash Light とボタンのセルです。) UISwitch *sw の行から cell.textLabel.text = @"Flash Light";の行までをswitch(indexPath.row)文の中に入れると、UISwitch *sw = ... の行でExpected expressionというエラーが出てしまうので 他のレイアウトを表示することができません。 解決策をお願いします。(_ _ )

  • UITableViewDataSourceエラー

    xcode 7.1 (7B91b)を使用しています。 SwiftでViewControllerにUItableViewを追加した時に、 クラスの定義でdoes not conform to protocol ‘UITableViewDataSource’エラーが発生します。 サイトで調べた結果2つのメソッドを記載することとして、下記を追加しましたが、 一向にエラーが取れません。他なにか要因がありますでしょうか?ご教示のほどよろしくお願いします。 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ } func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ return UITableViewCell() } 全体 class ShopListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{ } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ } func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ return UITableViewCell() }

    • ベストアンサー
    • Swift