【发布时间】:2015-03-12 06:39:37
【问题描述】:
好的,所以我使用 push segue 从我的CatViewController -> TopicViewController 中分离出来,我将 JSON 数据拉入 CVC 中的字典,并在 UITableView 中打印出来。一旦用户选择了一行,它应该从选定的NSDictionary 对象中提取categoryID,并使用一组不同的 JSON 数据填充下一个视图,其中replyID == categoryID。
显然不是这样,当我打印出我的passedData NSString 时,它显示为空。
我以为我正确构建了NSDictionary,然后获得了正确的信息,但它似乎并没有按照我的意愿去做。
CatViewController.m
- (void) retrieveData;
{
NSURL *url = [NSURL URLWithString:getDataURL];
NSData *data = [NSData dataWithContentsOfURL:url];
NSLog(@"%@", url);
jsonArray2 = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSError *error;
dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
for (catDict in dict) {
catID = catDict[@"categoryID"];
NSString *catDesc = catDict[@"categoryDesc"];
NSString *catName = catDict[@"categoryName"];
NSLog(catID);
}
categoryArray = [[NSMutableArray alloc] init];
for(int i = 0; i < jsonArray2.count; i++)
{
NSString *cID = [[jsonArray2 objectAtIndex:i] objectForKey:@"categoryID"];
NSString *cDesc = [[jsonArray2 objectAtIndex:i] objectForKey:@"categoryDesc"];
NSString *cName = [[jsonArray2 objectAtIndex:i] objectForKey:@"categoryName"];
[categoryArray addObject:[[Categories alloc]initWidthCategoryDesc:cDesc andcategoryName:cName andcategoryID:cID]];
}
[self.tableView reloadData];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"catToTopic"]) {
TopicViewController *tVC = (TopicViewController *)[segue destinationViewController];
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
tVC.passedData = [[[categoryArray objectAtIndex:indexPath.section] valueForKey:@"categoryID"] objectAtIndex:indexPath.row];
}
}
TopicViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
//test to see what gets passed along
NSLog(@"%@", passedData);
self.title = @"Test replies";
[self retrieveData];
UITableViewController *tvController = [[UITableViewController alloc] init];
tvController.tableView = self.tableView;
self.refreshControl = [[UIRefreshControl alloc] init];
[self.refreshControl addTarget:self action:@selector(updateTable:) forControlEvents:UIControlEventValueChanged];
tvController.refreshControl = self.refreshControl;
}
输出
2015-03-12 02:29:25.036 Aviato[3081:195303] Login SUCCESS
2015-03-12 02:29:25.314 Aviato[3081:195303] http://tandemenvoy.michaeldvinci.com/forum/categoriesJSON.php
2015-03-12 02:29:25.315 Aviato[3081:195303] 2
2015-03-12 02:29:25.315 Aviato[3081:195303] 5
2015-03-12 02:29:25.315 Aviato[3081:195303] 6
2015-03-12 02:29:25.316 Aviato[3081:195303] 7
2015-03-12 02:29:25.316 Aviato[3081:195303] 8
2015-03-12 02:29:25.316 Aviato[3081:195303] 9
2015-03-12 02:29:25.316 Aviato[3081:195303] 10
2015-03-12 02:29:25.316 Aviato[3081:195303] 11
2015-03-12 02:29:25.317 Aviato[3081:195303] 12
2015-03-12 02:29:25.317 Aviato[3081:195303] 13
2015-03-12 02:29:25.317 Aviato[3081:195303] 14
2015-03-12 02:29:25.317 Aviato[3081:195303] 15
2015-03-12 02:29:27.052 Aviato[3081:195303] (null)
2015-03-12 02:29:27.328 Aviato[3081:195303] http://tandemenvoy.michaeldvinci.com/forum/repliesJSON.php
【问题讨论】:
-
indexpath.section 和 indexpath.row 的值是多少?尝试使用硬编码值传递它\
-
你能发布jsonArray2吗?
-
2015-03-12 03:13:42.600 Aviato[3170:202855] ( { categoryDesc = "尝试实际类别"; categoryID = 2; categoryName = TestCat; }, { categoryDesc = " kjnk,m"; categoryID = 5; categoryName = qwert; }, { categoryDesc = hi; categoryID = 6; categoryName = trewq; }, { categoryDesc = m; categoryID = 7; categoryName = retrievethis;}, { categoryDesc = j; categoryID = 8; categoryName = qwqwqw; } (太长了)
-
你试过我的答案了吗?
标签: ios objective-c json nsdictionary segue