Я программирую приложение, я создал файлы MainTableviewVontrolller.h
и MainTableviewVontrolller.m
с соответствующим объектным файлом для Json, в котором хранятся данные с именами Youga.h
и Youga.m
в приведенном выше MainTableviewVontrolller.m
. Мне нужно извлечь некоторые данные из Интернета, и это работает отлично. теперь я должен перейти к другой сцене, содержащей DetailTableViewController.h
и DetailTableViewController.m
на раскадровке через Showdetail
Segue. Здесь я должен передать некоторые параметры, так как мне нужно выбрать те индексы в моем массиве, которые содержат ycategoryName, равно ранее выбранной строке MainTableviewVontroller.m путь к индексу yougaName
Я использую два разных веб-сервиса
http://yoga.lifehealthinfo.com/api/yoga_list/categories с идентификатором первичного ключа и имя
http://yoga.lifehealthinfo.com/api/yoga_list/all с соответствующей категорией ключей
category_id
иcategory_name
то, что я пытаюсь сделать, заключается в том, что я хочу показать соответствующие данные выбранной строки из второго Json-файла веб-сервисов. вот ошибка, которая у меня на консоли.
2016-08-10 11:54:07.904 iYouga[622:21074] -[UITableViewController getCategory:]: unrecognized selector sent to instance 0x7fb195808490
2016-08-10 11:54:07.951 iYouga[622:21074] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewController getCategory:]: unrecognized selector sent to instance 0x7fb195808490'
*** First throw call stack:
(
0 CoreFoundation 0x0000000103933e65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001033acdeb objc_exception_throw + 48
2 CoreFoundation 0x000000010393c48d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010388990a ___forwarding___ + 970
4 CoreFoundation 0x00000001038894b8 _CF_forwarding_prep_0 + 120
5 iYouga 0x0000000102ea90bf -[MainTableViewController prepareForSegue:sender:] + 351
6 UIKit 0x0000000104409f01 -[UIStoryboardSegueTemplate _performWithDestinationViewController:sender:] + 369
7 UIKit 0x0000000104409d5f -[UIStoryboardSegueTemplate _perform:] + 82
8 UIKit 0x000000010440a023 -[UIStoryboardSegueTemplate perform:] + 156
9 UIKit 0x0000000103e23cee -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1775
10 UIKit 0x0000000103e23fb3 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 388
11 UIKit 0x0000000103cec4a2 _runAfterCACommitDeferredBlocks + 317
12 UIKit 0x0000000103cffc01 _cleanUpAfterCAFlushAndRunDeferredBlocks + 95
13 UIKit 0x0000000103d0baf3 _afterCACommitHandler + 90
14 CoreFoundation 0x000000010385f367 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
15 CoreFoundation 0x000000010385f2d7 __CFRunLoopDoObservers + 391
16 CoreFoundation 0x0000000103854f2b __CFRunLoopRun + 1147
17 CoreFoundation 0x0000000103854828 CFRunLoopRunSpecific + 488
18 GraphicsServices 0x00000001070f0ad2 GSEventRunModal + 161
19 UIKit 0x0000000103ce0610 UIApplicationMain + 171
20 iYouga 0x0000000102ea98df main + 111
21 libdyld.dylib 0x000000010606f92d start + 1
22 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
А теперь просмотрите MainTableViewController.m
файл
#import "MainTableViewController.h"
#import "Youga.h"
#import "DetailTableViewController.h"
#define getDataUrl @"http://yoga.lifehealthinfo.com/api/yoga_list/categories"
@interface MainTableViewController ()
@end
@implementation MainTableViewController
@synthesize jsonArray, yougaArray;
- (void)viewDidLoad {
[super viewDidLoad];
[self retrieveData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return yougaArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
Youga * YougaObject;
YougaObject = [yougaArray objectAtIndex:indexPath.row];
cell.textLabel.text = YougaObject.yougaName;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// Configure the cell...
// Configure the cell...
return cell;
}
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
//get the object for selected row
Youga *object = [yougaArray objectAtIndex:indexPath.row];
[[segue destinationViewController] getCategory:object];
}}
-(void) retrieveData{
NSURL * url = [NSURL URLWithString:getDataUrl];
NSData * data = [NSData dataWithContentsOfURL:url];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSDictionary *dataJSON = [jsonDict objectForKey:@"data"];
NSArray *allCategoriesJSON = [dataJSON objectForKey:@"categories"];
yougaArray = [[NSMutableArray alloc] init];
for (int i = 0; i < allCategoriesJSON.count; i ++)
{
NSDictionary *aCategoryJSON = [allCategoriesJSON objectAtIndex:i];
NSString *yId = [aCategoryJSON objectForKey:@"id"];
NSString *yName = [aCategoryJSON objectForKey:@"name"];
NSString *yDescription = [aCategoryJSON objectForKey:@"description"];
NSString *yImage = [aCategoryJSON objectForKey:@"image"];
[yougaArray addObject:[[Youga alloc] initWithYougaId:yId andYougaName:yName andYougaDescpription:yDescription andYougaImage:yImage]];
}
[self.tableView reloadData];
}
@end
А вот и мой файл DetailTableViewController.m
#import "DetailTableViewController.h"
#import "Categories.h"
#import "MainTableViewController.h"
#define kUrl @"http://yoga.lifehealthinfo.com/api/yoga_list/all"
@interface DetailTableViewController ()
@end
@implementation DetailTableViewController
@synthesize categoryArray, currentCategory;
- (void)viewDidLoad {
[super viewDidLoad];
[self extractData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return categoryArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"maincell" forIndexPath:indexPath];
Categories * categoryObject;
categoryObject = [categoryArray objectAtIndex:indexPath.row];
cell.textLabel.text = categoryObject.ycategoryName;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// Configure the cell...
return cell;
}
-(void) getCategory:(id)yougaObject{
currentCategory = yougaObject;
}
-(void) extractData{
NSURL * curl = [NSURL URLWithString:kUrl];
NSData * cdata = [NSData dataWithContentsOfURL:curl];
NSDictionary *myDict = [NSJSONSerialization JSONObjectWithData:cdata options:kNilOptions error:nil];
NSDictionary *cdataJson = [myDict objectForKey:@"data"];
NSArray *allImagesJson = [cdataJson objectForKey:@"Images"];
categoryArray = [[NSMutableArray alloc]init];
for(int i = 0; i<allImagesJson.count; i++)
{
NSDictionary *aCategoryImages = [allImagesJson objectAtIndex:i];
NSString * cId = [aCategoryImages objectForKey:@"id"];
NSString * cTitle = [aCategoryImages objectForKey:@"tite"];
NSString * cDescription = [aCategoryImages objectForKey:@"description"];
NSString * cImage = [aCategoryImages objectForKey:@"image"];
NSString * cDate = [aCategoryImages objectForKey:@"date_created"];
NSString * ycName = [aCategoryImages objectForKey:@"category_name"];
NSString * ycId = [aCategoryImages objectForKey:@"category_id"];
[categoryArray addObject:[[Categories alloc]initWithCategoryId: cId andCategoryTitle:cTitle andCategoryDescription:cDescription andCategoryImage:cImage andCategoryDate:cDate andYcategoryId:ycId andYCategoryName:ycName]];
}
[self.tableView reloadData];
}
@end
Может кто решил эту проблему???