gpt4 book ai didi

ios - 已解析的 JSON 字符串未在 VC 之间共享

转载 作者:行者123 更新时间:2023-11-29 10:53:17 25 4
gpt4 key购买 nike

我正在从我的 API 中解析一堆字符串。其中之一是产品 ID。我想使用此 ID 来确定选择了哪个产品,以便我可以在详细信息 View Controller 中解析更多详细信息。

我现在遇到的问题是我在 TableView 中正确解析了 ID(它在日志中正确显示)但是当我将它推送到详细 View Controller 时,我得到了 (null) 作为那里的日志的响应。我不明白这一点。

在我的表格 View 中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *neuheitenCell = @"neuheitenCell";
CJHomeTableCell *cell = [tableView dequeueReusableCellWithIdentifier:neuheitenCell];
wareID = [[arrayArtikelWareID objectAtIndex:indexPath.row] objectForKey:@"ware_id"];
//NSLog(@"Selected item with id: %@",sstring);
return cell;
}

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
CJArtikelDetailViewController *artikelView = [[CJArtikelDetailViewController alloc] init];
NSString *test = [NSString stringWithFormat:@"%@", wareID];
NSLog(@"Test1: %@", wareID);
artikelView.wareID = test;
//NSLog(@"URL1: %@",test);
return indexPath;
}

提前致谢

最佳答案

像下面这样解析你的数据。我给出了 JSON 解析的例子。

NSDictionary* dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions  error:&error];
NSArray* locations = [dictionary objectForKey:@"results"];
if ([dictionary objectForKey:@"next_page_token"])
{
nextPageToken = [dictionary objectForKey:@"next_page_token"];
}
else
{
nextPageToken = @"";
}

for (int i = 0; i < [locations count]; i++)
{

xmlData =[[LocationModel alloc]init];
NSDictionary *locationDict = [locations objectAtIndex:i];

xmlData.name = [locationDict objectForKey:@"name"];
xmlData.formatted_address = [locationDict objectForKey:@"formatted_address"];

NSDictionary *geomentry = [[NSDictionary alloc]init];
geomentry = [locationDict objectForKey:@"geometry"];

NSDictionary *loc = [[NSDictionary alloc]init];
loc = [geomentry objectForKey:@"location"];

xmlData.lat = [loc objectForKey:@"lat"];
xmlData.lng = [loc objectForKey:@"lng"];
xmlData.urlString = [locationDict objectForKey:@"icon"];
[search_array addObject:xmlData];

}

cellForRowAtIndexPath

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
}
//Model class "LocationModel".
LocationModel *currentData;

currentData = [searchArray objectAtIndex:indexPath.row];

//Here I display the name. you display ur id.
cell.textLabel.text = [currentData name];
return cell;
}

then in **didSelectRowAtIndexPath**

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
LocationModel *currentData = [searchArray objectAtIndex:indexPath.row];

NSMutableArray *dataArray = [[NSMutableArray alloc]init];
[dataArray addObject:currentData];
CJArtikelDetailViewController *artikelView = [[CJArtikelDetailViewController alloc] init];
//Add detailArray in ur class CJArtikelDetailViewController
artikelView.detailArray = dataArray;
[self.navigationController pushViewController:artikelView animated:YES];
}

在你的 detailviewcontroller 中按你的模型类显示数据..

希望对您有所帮助。

关于ios - 已解析的 JSON 字符串未在 VC 之间共享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19561740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com