1

API から一連の文字列を解析しています。そのうちの 1 つが製品 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;
}

前もって感謝します

4

2 に答える 2

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 モデル クラスごとにデータを表示します。

お役に立てば幸いです。

于 2013-10-24T09:44:54.320 に答える