如何在有序列表中显示索引值并使用值

时间:2016-11-16 06:55:38

标签: ios objective-c json uitableview

我有两个部分,每个部分大约有10到17个问题, 当我使用json api将答案发布到服务器时, 结果显示在unorderlist中, 我的问题是如何在“Index_id”中显示来自服务器的“Question_id”。

- (IBAction)Submit:(id)sender
{
    AppDelegate *apd = [[UIApplication sharedApplication] delegate];

    NSUInteger arrayCount;

    NSMutableDictionary *question_array = [[NSMutableDictionary alloc]init];

    int k = 0;

    for (int i=0; i<dicc.count; i++) {
        NSMutableDictionary *answerDict = [[NSMutableDictionary alloc]init];

        arrayCount =  [[dicc valueForKey:[headersArray objectAtIndex:i]]count];

        for (int j=0; j<arrayCount; j++)
        {
            int x = k + j;
            NSString *key = [NSString stringWithFormat:@"%d", j];

            answerDict[key] =  take_five_ans_arr[x];

        }

        //   NSString * key = [[dicc valueForKey:[headersArray objectAtIndex:i]]valueForKey:@"question_id"];  // get question id
        NSString *key = [NSString stringWithFormat:@"%d", i];

        question_array[key] = answerDict;
        k += arrayCount;
    }

    NSString *post = [NSString stringWithFormat:@"jobid=%@&id=%@&questionarray=%@",apd.jobid2,user_idis,question_array];

    NSLog(@"%@",post);

    // NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
    NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

    [request setURL:[NSURL URLWithString:@"http://10.10.1.13//webservices_api/process"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    if(conn)
    {
        NSLog(@"Connection Successful");
    }
    else
    {
        NSLog(@"Connection could not be made");
    }
}

我得到的输出是:

questionarray=
{
    0 =     {
        0 = 1;
        1 = 1;
        10 = 1;
        11 = 1;
        12 = 1;
        13 = 1;
        14 = 1;
        15 = 1;
        16 = 1;
        2 = 1;
        3 = 1;
        4 = 1;
        5 = 1;
        6 = 1;
        7 = 1;
        8 = 1;
        9 = 1;
    };
    1 =     {
        0 = 0;
        1 = 0;
        10 = 0;
        11 = 0;
        12 = 0;
        13 = 0;
        14 = 0;
        15 = 0;
        2 = 0;
        3 = 0;
        4 = 0;
        5 = 0;
        6 = 0;
        7 = 0;
        8 = 0;
        9 = 0;
    };

但我想显示indexrowid“Question_id”,它将从api中获取, 我的Json api是:

{"question_id":"27","question_title":"Do I need mechanical lifting or a work at height platform? .. ","question_answer":"No","question_type":"Full"}

1 个答案:

答案 0 :(得分:0)

现在取一个可变数组并初始化,然后从json数据中获取问题id,然后放在索引号之前。

&#13;
&#13;
    NSUInteger arrayCount;
    
    NSMutableArray * qid = [[NSMutableArray alloc] init];
    
    NSMutableDictionary *question_array = [[NSMutableDictionary alloc]init];

    int k = 0;
    
    for (int i=0; i<dicc.count; i++) {
        
 qid = [[dicc valueForKey:[headersArray objectAtIndex:i]]valueForKey:@"question_id"];

        NSMutableDictionary *answerDict = [[NSMutableDictionary alloc]init];
        
        arrayCount =  [[dicc valueForKey:[headersArray objectAtIndex:i]]count];
        
        for (int j=0; j<arrayCount; j++)
        {
            int x = k + j;
            
             NSString * key1 = [[dicc valueForKey:[headersArray objectAtIndex:i]]valueForKey:@"question_id"];

          NSString *key = [NSString stringWithFormat:@"%d, %@", j,qid[j]];
            
            answerDict[key] =  take_five_ans_arr[x];
        }

NSString *key = [NSString stringWithFormat:@"%d", i];
        
        question_array[key] = answerDict;
        
        k += arrayCount;
&#13;
&#13;
&#13;