質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Objective-C

Objective-Cはオブジェクト指向型のプログラミング言語のひとつです。C言語をベースにSmalltalkが取り入れられています。

iOS 9

iOS 9は、アップル社のモバイルOSであるiOSシリーズのバージョン。特徴として検索機能の強化、Siriの機能改良、iPad向けマルチタスクなどがあります。マルチウィンドウ機能をサポートし、iPad向けマルチタスクもサポートされています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Q&A

解決済

1回答

1873閲覧

APIでJSON情報を取得するコードをawakefromnibに書くと落ちる

maplesugar_17

総合スコア32

Objective-C

Objective-Cはオブジェクト指向型のプログラミング言語のひとつです。C言語をベースにSmalltalkが取り入れられています。

iOS 9

iOS 9は、アップル社のモバイルOSであるiOSシリーズのバージョン。特徴として検索機能の強化、Siriの機能改良、iPad向けマルチタスクなどがあります。マルチウィンドウ機能をサポートし、iPad向けマルチタスクもサポートされています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

0グッド

0クリップ

投稿2016/07/28 21:49

編集2016/07/29 05:22

Objective-cでAPIでJSON情報を取得する機能を作っています。
実装中に以下のエラーメッセージが発生しました。

###発生している問題・エラーメッセージ

> *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView (<UITableView: 0x16145a00; frame = (0 30; 414 140); transform = [0, -1, 1, 0, 0, 0]; clipsToBounds = YES; gestureRecognizers = <NSArray: 0x1568e200>; layer = <CALayer: 0x1568ded0>; contentOffset: {0, 0}; contentSize: {140, 7560}>) failed to obtain a cell from its dataSource (<VerticalTableViewCell: 0x16143a00; baseClass = UITableViewCell; frame = (0 170; 414 170); autoresize = W; layer = <CALayer: 0x1568d6d0>>)' > *** First throw call stack: > (0x22369b0b 0x21b26dff 0x223699e1 0x22b5073b 0x26a94663 0x26c91e89 0x26c91f61 0x26c8143d 0x26c96747 0x26a33a6f 0x269446fb 0x249b5569 0x249b0d2d 0x2695ad53 0x26a07039 0x26a04ccf 0x26a1dc23 0x26a1d983 0x26a1d6cf 0x269835e1 0x2698191d 0x26a106b5 0x26a0ffeb 0x26a06805 0x26a05aa7 0x26a05701 0x26a05677 0x269446fb 0x249b5569 0x249b0d2d 0x249b0bbd 0x249b0081 0x249afd55 0x249a94ff 0x2232b2b1 0x223295a7 0x222781e9 0x22277fbd 0x269b6f37 0x269b1435 0xbe34b 0x21f24873) > libc++abi.dylib: terminating with uncaught exception of type NSException

failed to obtain a cell from its dataSource と書いてあるので、cellのidentifierが間違っているのかと思い、確認しましたが、ちゃんと書いてあります。

###該当のソースコード

static NSString * const TableViewCustomCellIdentifier = @"Cell"; @implementation VerticalTableViewCell -(void)awakeFromNib{ [super awakeFromNib]; UINib *nib = [UINib nibWithNibName:TableViewCustomCellIdentifier bundle:nil]; [self.horizontalTableView registerNib:nib forCellReuseIdentifier:@"Cell"]; if(imgcache){ } else{ imgcache = [[NSCache alloc] init]; } weather_icon = @{//day @"01d":[UIImage imageNamed:@"01d.png"], @"02d":[UIImage imageNamed:@"02d.png"], @"03d":[UIImage imageNamed:@"03d.png"], @"04d":[UIImage imageNamed:@"04d.png"], @"09d":[UIImage imageNamed:@"09d.png"], @"10d":[UIImage imageNamed:@"10d.png"], @"11d":[UIImage imageNamed:@"11d.png"], @"13d":[UIImage imageNamed:@"13d.png"], @"50d":[UIImage imageNamed:@"50d.png"], //night @"01n":[UIImage imageNamed:@"01n.png"], @"02n":[UIImage imageNamed:@"02n.png"], @"03n":[UIImage imageNamed:@"03n.png"], @"04n":[UIImage imageNamed:@"04n.png"], @"09n":[UIImage imageNamed:@"09n.png"], @"10n":[UIImage imageNamed:@"10n.png"], @"11n":[UIImage imageNamed:@"11n.png"], @"13n":[UIImage imageNamed:@"13n.png"], @"50n":[UIImage imageNamed:@"50n.png"]}; // horizontalのセルを生成 static NSString *CellIdentifier = @"Cell"; Cell *cell_object = [table dequeueReusableCellWithIdentifier:CellIdentifier]; if(cell_object == nil){ cell_object = [Cell loadFromNib]; } // この部分が重要 dispatch_queue_t q_global = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_queue_t q_main = dispatch_get_main_queue(); cell_object.weather_icon_image.image = nil; // 実行待ち dispatch_async(q_global, ^{ NSRange searchResult = [self.weather_capital[indexp] rangeOfString:@"lat="]; if(searchResult.location == NSNotFound){ url = [NSURL URLWithString:[NSString stringWithFormat:@"http://api.openweathermap.org/data/2.5/weather?APPID=b8f4ce09ae1ca4d1b34a14438e857866&q=%@", self.weather_capital[indexp]]]; }else{ url = [NSURL URLWithString:[NSString stringWithFormat:@"http://api.openweathermap.org/data/2.5/weather?APPID=b8f4ce09ae1ca4d1b34a14438e857866&%@", self.weather_capital[indexp]]]; } NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; if(![imgcache objectForKey:url]){ [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { if (data) { NSDictionary *object = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error:nil]; if (object) { // ⑦ weather.mainの値を抽出 c++; NSArray *main = [object valueForKeyPath:@"weather.main"]; //天候 NSArray *description = [object valueForKeyPath:@"weather.description"]; // 天候詳細 NSArray *speed = [object valueForKeyPath:@"wind.speed"]; //風速 NSArray *icons = [object valueForKeyPath:@"weather.icon"]; NSArray *names = [object valueForKeyPath:@"name"]; NSLog(@"main(天候)=%@,description(天候詳細)=%@,speed(風速)=%@,icons(天気アイコン)=%@,name=%@",main,description,speed,icons,names); NSMutableDictionary *weather= @{@"main":main, @"description":description, @"speed":speed, @"icons":icons, @"names":names}.mutableCopy; dispatch_async(q_main, ^{ NSString *imageKeyname = weather[@"icons"][0]; cell_object.weather_icon_image.image = weather_icon[imageKeyname]; iconimg = weather_icon[imageKeyname]; [imgcache setObject:iconimg forKey:url]; [cell_object layoutSubviews]; NSString *cityname = weather[@"names"]; cell_object.title.text = cityname; }); }else { } }else { NSLog(@"レスポンス == %@, エラー == %@", response, error); } }]; } else{ NSLog(@"cache...%@",imgcache); cell_object.weather_icon_image.image = [imgcache objectForKey:url]; } }); cell_object.title.text = [NSString stringWithFormat:@"%@",_weather_capital[indexp]]; table.separatorStyle = UITableViewCellSeparatorStyleNone; table.bounces = YES; if (indexp % 2 == 0) { cell_object.backgroundColor = [UIColor colorWithHue:0.0 saturation:0.0 brightness:0.67 alpha:1.0]; } // 奇数セル else { cell_object.backgroundColor = [UIColor colorWithHue:0.0 saturation:0.0 brightness:0.73 alpha:1.0]; } // セルの向きを横向きに cell_object.contentView.transform = CGAffineTransformMakeRotation(M_PI / 2); NSLog(@"%d",c); } -(Cell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; Cell *cell_object = [table dequeueReusableCellWithIdentifier:CellIdentifier]; table = tableView; indexp = indexPath.row; return cell_object; }

###試したこと
Cell.xibのIdentifierを確認しました。cell_objectになっていたので、合っていると思います。また、ViewWillAppearにしても同じ挙動が発生し、ダミーの情報に変えてみましたがそれも同じ挙動です。 やはりTableViewの読み込み時のCellがおかしいのでしょうか?なお、Cellはカスタムです。指摘してくださいました_Kentarou様、ありがとうございます。

###補足情報(言語/FW/ツール等のバージョンなど)
Objective-c,Xcode7.3.1,IOS9.3.3です。 実機でシミュレートしています。
よろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

_Kentarou

2016/07/29 00:30

質問ですが、こちらのコードはawakeFromNibではなく、viewDidAppearとかで実行した場合は問題ないですか? あと、取得するのではなくてダミーのデータを流しこんだら問題なく動きますか?
maplesugar_17

2016/07/29 00:34

ViewWillAppearで最初やっていたのですが、それでも落ちていたので、awakefromnibに変更しました。 viewWillAppearでやっていた方が良いのでしょうか?ダミーのデータを流し込むのは、やってみていません。アドバイスをありがとうございます、早速シミュレートしてみます。
maplesugar_17

2016/07/29 00:41

ダミーでもうまくいかず、同じエラーを吐いて落ちてしまいました。 ということは、やはりcellでうまくいかないのが原因ですね。 すこし自分でも調べてみます。ご教示お願いします。
_Kentarou

2016/07/29 03:25

何かのサンプルを参考にしていますか?あまりこのような書き方を見たことがないので、、、とりあえず最低限テーブル、セルが普通に表示されてから細かい実装をしたほうが早いと思います。
maplesugar_17

2016/07/29 03:50

アプリ自体はすべて完成しています。テーブルビューセルの中にもう一つ横スクロールのテーブルビューを入れています。
fuzzball

2016/07/29 04:27

「CellのIdentifierを確認しました。cell_objectになっていたので、合っていると思います。」というのはどういう意味でしょうか?コードではCellのidentifierは"Cell"になっていますが。
maplesugar_17

2016/07/29 04:28

ごめんなさい、cell.xibのセルです。cell_object ではなく、Cellにすべきなのでしょうか?
fuzzball

2016/07/29 04:31

コードの一番最後にある、static NSString *CellIdentifier = @"Cell"; Cell *cell_object = [table dequeueReusableCellWithIdentifier:CellIdentifier]; のセルですよね?"Cell"を指定しているのだから"Cell"にしないとダメなんじゃないでしょうか?
maplesugar_17

2016/07/29 04:33

そうなのですね!! ありがとうございます、早速試してみます!!
maplesugar_17

2016/07/29 04:36

そうしてみましたが、やはり同じエラーを吐いてlaunchの画面で落ちてしまいました。 static NSString *CellIdentifier = @"Cell"; Cell *cell_object = [table dequeueReusableCellWithIdentifier:CellIdentifier]; というのは、もしかしたらそのセルのidentifierに設定するという事なのでしょうか…? そうしたらやはり空欄の方がいいのでしょうか? でも空欄でも落ちてしまいます… アドバイス、ありがとうございました。
fuzzball

2016/07/29 04:55

xib上のidentifierとコード上のidentifierを一致させることで紐付けを行っています。なので空欄はダメです。
maplesugar_17

2016/07/29 04:57

そうなのですか…。 ありがとうございます。 では、static NSString *CellIdentifier = @"Cell"; Cell *cell_object = [table dequeueReusableCellWithIdentifier:CellIdentifier]; の意味は、そのセルのidentifierに設定するという事ではないということですか?
fuzzball

2016/07/29 05:04

セルにidentifierを設定しているのではなく、そのidentifierを持つセルを取得する、という意味です。
maplesugar_17

2016/07/29 05:07

そういうことなのですね! ご教示ありがとうございました。
guest

回答1

0

ベストアンサー

awakeFromNibの中でindexptableを使っていますが、この時点でcellForRowAtIndexPathは呼ばれていないのでindexptableも初期化されていません。

cellForRowAtIndexPathのdequeueReusableCellWithIdentifierですが、tableではなくtableViewでしょう。

objectivec

1x [table dequeueReusableCellWithIdentifier:CellIdentifier] 2o [tableView dequeueReusableCellWithIdentifier:CellIdentifier]

などなど、おかしなところが多々あり、アプリが落ちる以前の問題です。
_Kentarouさんもアドバイスされていますが、もっとシンプルなものでUITableViewの基本を勉強した方が良いと思います。

投稿2016/07/29 05:40

fuzzball

総合スコア16731

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

maplesugar_17

2016/07/29 20:41

fuzzballさんがおっしゃった、[table dequeueReusableCellWithIdentifier:CellIdentifier] の件ですが、tableというのはtableviewを代入してあるグローバル変数、UITableViewです。 >この時点でcellForRowAtIndexPathは呼ばれていないのでindexpもtableも初期化されていません。 そうなのですね!ありがとうございます。しかし、もう一つ、tableviewを作っている方が、breakpoint を置いたところ先に読み込まれていたので、tableとindexpをpropertyに変え、そこで代入してみました。しかし、まだ落ちてしまいます。 何か間違っていますでしょうか?ご教示お願いします。
fuzzball

2016/07/30 07:13 編集

>>tableというのはtableviewを代入してあるグローバル変数、UITableViewです。 それは分かっていますが、そうすること自体が間違っているのではないか?という指摘です。 根本的に色々と間違っているので、小手先で修正しても直らないと思いますよ。もう一度書きますが、まずUITableViewの使い方を理解して下さい。
fuzzball

2016/08/01 00:13 編集

tableがグローバル変数ということですが、どこでどういう風に定義しているか教えて下さい。 また、Viewの構成は下記のような感じで合っているでしょうか? TableViewController (or ViewController+TableView) └VerticalTableViewCell  └HorizontalTableView   └Cell
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問