如何在iphone中显示从web服务获取的表视图数据

如何在iphone中显示从web服务获取的表视图数据,iphone,objective-c,web-services,uitableview,ios5,Iphone,Objective C,Web Services,Uitableview,Ios5,我正在使用以下代码从web服务获取数据 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. recordResults = FALSE; NSString *soapMessage = [NSString stringWithFormat: @"<?xml vers

我正在使用以下代码从web服务获取数据

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    recordResults = FALSE;

    NSString *soapMessage = [NSString stringWithFormat:
                             @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                             "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
                             "<soap:Body>\n"
                             "<GetContacts xmlns=\"http://tempuri.org/\">\n"
                             "<loginid>nareshreddyyaradla.net@gmail.com</loginid>\n"
                             "</GetContacts>\n"
                             "</soap:Body>\n"
                             "</soap:Envelope>\n",loginId.text
    ];

    NSLog(soapMessage);

    NSURL *url = [NSURL URLWithString:@"http://www.xxxxxx.net/services/contacts.asmx"];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%d",[soapMessage length]];

    [theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [theRequest addValue:@"http://tempuri.org/GetContacts" forHTTPHeaderField:@"SOAPAction"];
    [theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    NSHTTPURLResponse *urlResponse = nil;
    NSError *error = [[NSError alloc] init];
    NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&urlResponse error:&error];

    NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    NSLog(@"Response Code: %d",[urlResponse statusCode]);
    if([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300)
    {
        NSLog(@"Response: %@", result);
    }


    if( theConnection )
    {
        webData = [NSMutableData data];
    }
    else
    {
        NSLog(@"theConnection is NULL");
    }
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSURLResponse *)response
{
    [webData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"ERROR with theConnection");
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"DONE. Received Bytes: %d", [webData length]);
    NSString *theXML = [[NSString alloc] initWithBytes:[webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
    NSLog(theXML);

    if( xmlParser )
    {

    }

    xmlParser = [[NSXMLParser alloc] initWithData:webData];
    [xmlParser setDelegate:self];
    [xmlParser setShouldResolveExternalEntities:YES];
    [xmlParser parse];
}

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if([elementName isEqualToString:@"GetContactsResult"])
    {
        if(!soapResults)
        {
            soapResults = [[NSMutableString alloc] init];
        }
        recordResults = TRUE;
    }
}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if( recordResults )
    {
        [soapResults appendString:string];
    }
}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if([elementName isEqualToString:@"GetContactsResult"])
    {
        recordResults = FALSE;
        greeting.text = soapResults;
        soapResults = nil;
    }
}
我需要在UITableView中显示表名称,我遵循了以下示例, 但我无法在tableView上显示它

TableView代码

- (void)parserDidEndDocument:(NSXMLParser *)parser{

    UITableView *tab = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain];

    tab.delegate = self;
    tab.dataSource = self;

    [self.view addSubview:tab];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [namedata count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [[UITableViewCell alloc] init];    
    cell.textLabel.text=[arrareasdata objectAtIndex:indexPath.row];
    cell.imageView.image=[array objectAtIndex:indexPath.row];
    tableView.separatorColor=[UIColor greenColor];
    cell.textLabel.textColor=[UIColor blackColor];

    return cell;

}
任何建议或链接将不胜感激,
提前感谢。

尝试重新加载uitableview
[tableview重新加载数据]从web服务获取数据的那一刻

不要太具体。您实际面临的问题是什么。您能够解析响应吗?您是如何向表视图提供数据的?@Hariprasad,请检查我编辑的代码,我已经添加了表视图代码。
- (void)parserDidEndDocument:(NSXMLParser *)parser{

    UITableView *tab = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain];

    tab.delegate = self;
    tab.dataSource = self;

    [self.view addSubview:tab];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [namedata count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [[UITableViewCell alloc] init];    
    cell.textLabel.text=[arrareasdata objectAtIndex:indexPath.row];
    cell.imageView.image=[array objectAtIndex:indexPath.row];
    tableView.separatorColor=[UIColor greenColor];
    cell.textLabel.textColor=[UIColor blackColor];

    return cell;

}