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です。 実機でシミュレートしています。
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー