开发者

parsing xml in UITableViewCell after receiving response from server

Hello friend, I want ask something how I can return Conn.xmlParser because the connectionDidFinishLoading is void and I need this result in order parsing in UITableView how I can use the value of Conn.mlParser?

Please need help.

 -(void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
        NSLog(@"DONE. Received Bytes: %d", [Conn.webData length]);
        ta=Conn.webData;
        if( Conn.xmlParser )
        {
            [Conn.xmlParser release];
        }

        Conn.xmlParser = [[NSXMLParser alloc] initWithData: Conn.webData];

        [Conn.xmlParser setDelegate: self];

        [Conn.xmlPars开发者_StackOverflow社区er setShouldResolveExternalEntities: YES];
        [Conn.xmlParser parse];

        [connection release];
        [Conn.webData release];
    }


Make sure your delegate also implements the NSXMLParserDelegate protocol. Then you can call didStartElement, didEndElement, didEndDocument, etc.

I usually create an NSArray (or NSMutableArray) that then is populated by objects of the type you are retrieving in the webdata using the NSXMLParser methods mentioned above. Use this array as the datasource in the UITableView's cellForRowAtIndexPath also.

When you reach didEndDocument just call [myUITableView reload] to update the table view. (Unless the tableview and the connection delegate are in two different classes, then you should use KVO to let the tableviewdelegate know the data has been retrieved).

FOLLOW UP:

Rather then just give you are example (of which there are already several on the web), which will just leave you with more questions, I'm going to try to explain what it is that you should understand if you are going to be a successful iOS developer.

And if the steps below seem too daunting for you I suggest you step back and try to tackle the problem slowly one piece at a time. If you still aren't making progress, then go buy a copy of Beginning iPhone 4 Development by Mark, Nutting and LaMarche and then come back to this when you understand the underlying process of Objective-C and iPhone programming better.

To accomplish your goal:

First, you should ask yourself what you are trying to accomplish.

You are getting data from a website (webservice perhaps?) and you want to decode (parse) that information and display it in a tableview. Correct?

So, to solve your problem you have to break apart the question and make sure you understand how to do each part of your task.

  1. Create a seperate Class modeled after the type of data you are going to recieve from your website. If you are getting car objects that specify color, year, model then you should create a Car class with color, year and model properties.

  2. Get Data from a website. You seem to have some idea how to do this since you are already capturing a connection event in the code you showed above. However, do you understand what is happening in ConnectionDidFinishLoading?

  3. Parse the data returned from the website. This requires the use of an XML parser, again you seem to at least know that you need to do something here to parse the data. It also requires a place to store the data that is returned from the website. An array of objects of the type you create in step 1 will be your storage area for the returned and parsed data. Finally, it requires that you understand the specifics of the data that is being passed to you from the website so you understand how to parse the data and store it correctly (and that it really is in XML format, else XMLparsing will not work here).

  4. Display the parsed data in your UITableView. This requires you to understand how to display data in a tableview and how to set the datasource for the tableview.

Second, you should look for sample code you can study that does what you are trying to accomplish and read Apple's documentation to get a firm grip on what has to happen to create your desired result. Look at each part of your task and wherever there is something that is unclear, study that specific issue. How do I use NSXMLParser? How do I display data in a UITableView from an array? etc.

If you don't take the time to really understand how to solve this problem, rather then just copying code, then you will just be back tomorrow with another problem.

Hopefully this will help you solve this problem and help you understand how to solve future problems.

And to get you started, I will give you a link that more or less answers your question.

iPhone Tutorial Creating an RSS feed Reader

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜