开发者

display contents of array in table view

I am accessing a web service, where i give the start date and end date and in return i get a string array from web service. each string from the string array is in this Format "1|Bank Name|Account NO|121|Drawer Name". now i want to display this content in the first row of the table view. the second row should be occupied with the second string of String array. I tried in the below manner, but my table seems to be empty.Please help.

#import "RootViewController.h"
@implementation RootViewController

    - (void)viewDidLoad {
    [super viewDidLoad];

    recordResults = FALSE;

    NSString *soapMessage = [NSString stringWithFormat:
                             @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                             "<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
                             "<S:Header/\>\n"
                             "<S:Body>\n"
                             "<ns2:reviewDeposit xmlns:ns2=\"http://services.cbp.syntel.org/\">\n"
                             "<FromDate>%@</FromDate>\n"
                             "<ToDate>%@</ToDate>\n"
                             "</ns2:reviewDeposit>\n"
                             "</S:Body>\n"
                             "</S:Envelope>\n", @"Sep 10, 2009", @"Dec 10, 2009"
                             ];

    .........bla bla
}


    /*.....methods for accessing web service*/

    -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName attributes: (NSDictionary *)attributeDict{


    if( [elementName isEqualToString:@"return"])
    {
        if(!soapResults)
        {
            soapResults = [[NSMutableString alloc] init];
        }
        recordResults = TRUE;
    }}
    -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    if( recordResults )
    {
        //DFSSoapTestAppDelegate *appdel=(DFSSoapTestAppDelegate *)[[UIApplication sharedApplication]delegate];

        [soapResults appendString: string];
    }}
    -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    if( [elementName isEqualToString:@"return"])
    {

        recordResults = FALSE;

        //chunks=[soapResults componentsSeparatedByString:@"|"];



        //NSString *s=[chunks objectAtIndex:0];

        if([soapResults isEqualToString:@"Error"]){
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Sorry Please Refine Your Search"
                                                           delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];
            [alert release];
            startdate.text=@"";
            enddate.text=@"";

        }else {

            [chunks addObject:soapResults];//where chunks is a NSMutable array

            NSLog(@"The Soap Results are.....");
            NSLog(soapResults);// "1|Bank Name|Account NO|121|Drawer Name"

        }
    } 

    /
}



    - (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

    - (void)viewDidUnload {
    // Release anything that can be recreated in viewDidLoad or on demand.
    // e.g. self.myOutlet = nil;
}




    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {return 1;}



    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return [chunks count];}


    - (UITableViewCell *)tableView:(UITabl开发者_开发百科eView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell.
    NSString *cellValue=[chunks objectAtIndex:indexPath.row];
    cell.text =cellValue;
    return cell;
}



- (void)dealloc {
    [super dealloc];
}


@end


You will need to [tableView reloadData] when the parser has completed.


It might have to do with where you call your parser. If the parser has not run, then the table will be blank. You don't show the code that forces an update of the table when there is data. Its possible that 'tableView:numberOfRowsInSection:' is simply never being called after the view's initial loading.

I would recommend logging chucks at the end of the parser to make sure it actually has values in it. You could also log it right before you populate the cells.

I would suggest moving the parser and methods for connecting to the web services into a different object than your table controller. In general, you want to separate the fetching/formatting/management of data from both the controller and the view. In this specific case, you don't want the controller tied up waiting for some reply when it needs to update the table. Instead, you should have a model object that just hands the controller an array. This will also make it easy to deal with errors related to fetching the data.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜