简体   繁体   中英

How can i load a TableView with a NSMutableArray that contains NSMutableDictionary Object?

I am parsing xml file and adding the attribute values in a NSMutableArray as follows :

-(NSMutableArray *) ReadXml
{
    NSError *error=[[NSError alloc] init];
    CXMLDocument *doc=[[CXMLDocument alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://socialcubebd.com/pandit/questions.xml"] 
                                                          options:0 
                                                          error:&error];

    if (error==nil) {


        //CXMLDocument *doc=[[CXMLDocument alloc] init];
        //doc=[self ReadXml];
        NSMutableArray *questionsWithQuestionDictionary = [[NSMutableArray alloc] init];
        //  we will put parsed data in an a array
        NSArray *nodes = NULL;
        //  searching for question nodes
        nodes = [doc nodesForXPath:@"//question" error:nil];
        NSMutableDictionary *question ;

        for (CXMLElement *node in nodes) {
            question= [[NSMutableDictionary alloc] init];
            int counter;

            for(counter = 0; counter < [node childCount]; counter++) {

                //  common procedure: dictionary with keys/values from XML node
                [question setObject:[[node childAtIndex:counter] stringValue] forKey:[[node childAtIndex:counter] name]];
            }

            //  and here it is - attributeForName! Simple as that.
            [question setObject:[[node attributeForName:@"number"] stringValue] forKey:@"number"];
            [question setObject:[[node attributeForName:@"question"] stringValue] forKey:@"question"];
            [question setObject:[[node attributeForName:@"questionImage"] stringValue] forKey:@"questionImage"];
            [question setObject:[[node attributeForName:@"option4"] stringValue] forKey:@"option4"]; 
            [question setObject:[[node attributeForName:@"option1"] stringValue] forKey:@"option1"]; 
            [question setObject:[[node attributeForName:@"option2"] stringValue] forKey:@"option2"];
            [question setObject:[[node attributeForName:@"option3"] stringValue] forKey:@"option3"]; 



            [questionsWithQuestionDictionary addObject:question];
            [question release];
        }


    return questionsWithQuestionDictionary; 
    }

    else
    {
        return 0;
    }


}

//---------------------------------------

Resulting NSMutableArray with NSMutableDictionary is Like :

2011-04-15 18:11:22.647 SC.Pandit[983:207] (
        {
        number = 1;
        option1 = "Richard Marx";
        option2 = "Bon Jovi";
        option3 = "Bryan Adams";
        option4 = "None of them";
        question = "Who is he ?";
        questionImage = "http://photographerscube.com/MediumSizePhoto/medium_1.jpg";
    },

{
        number = 2;
        option1 = USA;
        option2 = UAE;
        option3 = UK;
        option4 = Spain;
        question = "Which countries currency known as \U2018Pound\U2019?";
        questionImage = "http://photographerscube.com/MediumSizePhoto/medium_2.jpg";
    },

 {
        number = 3;
        option1 = Taka;
        option2 = Rupee;
        option3 = "Austral ";
        option4 = Dollar;
        question = "What is the name of the currency of Argentina?";
        questionImage = "http://photographerscube.com/MediumSizePhoto/medium_3.jpg";
    },

{
        number = 4;
        option1 = Euro;
        option2 = Dirham;
        option3 = Dinar;
        option4 = Dollar;
        question = "Identify the currency name from the picture-";
        questionImage = "http://photographerscube.com/MediumSizePhoto/medium_4.jpg";
    },

{
        number = 5;
        option1 = Sweden;
        option2 = Qatar;
        option3 = "Bangladesh ";
        option4 = Egypt;
        question = "Krona\U2019 is the name of currency of \U2013";
        questionImage = "http://photographerscube.com/MediumSizePhoto/medium_5.jpg";
    }
)

///////////////////////////////////////////////////

Now i want to Load my tableView with every single dictionary dictionary separately.Can anyone help me on this?

我认为结果是一个字典数组,您可以在UITableView的cellForRowAtIndexPath:方法中访问

[[yourArray objectAtIndex:indexPath.row]objectForKey:keyInDictionary];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM