我正在从我的API解析一堆字符串。其中一个是产品ID。我想使用此ID来确定选择了哪个产品,以便我可以在详细视图控制器中解析更多详细信息。
我现在遇到的问题是我在表视图中正确解析了ID(它在日志中正确显示)但是当我将它推送到详细视图控制器时,我得到(null)
作为来自日志的响应。我不明白这一点。
在我的表格视图中:
- (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;
}
提前致谢
答案 0 :(得分:2)
解析您的数据,如下所示。我在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];
}
在Ur detailviewcontroller 中按ur模型类显示数据..
我希望它对你有所帮助。
答案 1 :(得分:0)
这不属于JSon解析。问题只是将String传递给另一个VC。
要注意这些:
CJArtikelDetailViewController.h
@property(Strong, nonatomic) NSstring * wareID;
在您的当前VC中,只需从方法didSelectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
CJArtikelDetailViewController *artikelView = [[CJArtikelDetailViewController alloc] init];
// NSString *test = [NSString stringWithFormat:@"%@", wareID];
NSLog(@"Test1: %@", wareID);
artikelView.wareID = wareID;
}