Question

I am new to iPhone,as per requirement I have to select a single question from list of question in the tableview,and want to show that question in the next new view.so I send my row value to new Xib file as the time of creation.

I stored my question-ans in plist as a array of dictionary format.I can fetch the array from Plist,but unable to fetch dictionary data..I made my custom class to store question-Answer.but at the new get only the last question that is stored in the plist. Please help me.thank u..

Here I am attaching my code tableView.m,newView.m and customClass.m..

tableView.m

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {

            NSLog(@" index row value:%d",indexPath.row);
           NSInteger row=indexPath.row;
            questionViewController *viewController1 =
            [[questionViewController alloc] initWithNibName:@"questionViewController" bundle:[NSBundle mainBundle]];
            viewController1.rowValue=row;
                self.viewController = viewController1;
            [self.viewController.navigationItem setTitle:@"Ques Details"];


        [self.navigationController pushViewController:self.viewController animated:YES];
    }

in The new Xib File,In ViewDidLoadFunction

newView.m

    - (void)viewDidLoad
    {
      //rowValue tells me which row is selected
        [self firstView:rowValue];

    }
    -(void)firstView: (NSInteger)indexNumber{
        NSString *thePath = [[NSBundle mainBundle]  pathForResource:@"Qus_ans" ofType:@"plist"];
        NSArray *rawElementsArray = [[NSArray alloc] initWithContentsOfFile:thePath];
        NSDictionary *eachElement;
     //overlap the data here   
     for (eachElement in rawElementsArray)
        {
           element = [[Question alloc] initWithDictionary:eachElement];}
   //get only last question in the plist
   questionLabel.text =[NSString stringWithFormat:@"%@",element.ques];

     }
    element is my Custom class object.This is my custom class:
    Question.m

    - (id)initWithDictionary:(NSDictionary *)aDictionary {

        self = [[Question alloc] init];
        if (self) {

            self.ques = [aDictionary valueForKey:@"question"];
            self.ans = [aDictionary valueForKey:@"answer"];

            [self data];
        }
        return self;
    }

    Here element.ques is overlapped with the last value.
    How I get proper question without using the forLoop...

This is my plist format

<array>
    <dict>
        <key>question</key>
        <string>Approximately how much does the human brain weigh?</string>
        <key>answer</key>
        <string>Three pounds</string>
    </dict>
    <dict>
        <key>question</key>
        <string>The brain has this number of The brain has this number of nerve cells?</string>
        <key>answer</key>
        <string>1 to 100 billion</string>
    </dict>
Was it helpful?

Solution

Well, I'm not entirely sure I undestand your problem, but if you are trying to get rid of your for loop (which I guess is the case), you could simply write

NSArray *rawElementsArray = [[NSArray alloc] initWithContentsOfFile:thePath];
NSDictionary * dic = rawElementsArray objectAtIndex:rowNumber];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top