我的程序在tableView之前的NSURLConnection中不起作用

时间:2012-09-11 08:36:26

标签: ios xcode wsdl nsurlconnection tableview

为什么我的程序在tableView之前不能在NSURLConnection中工作?

//  MyLoxFileTVContoller.m

#import "MyLoxFileTVContoller.h"
#import "xml_myloxfile.h"
#import "DetailViewController.h"

@implementation MyLoxFileTVContoller
@synthesize myloxfileArray;
@synthesize exportId_data_Array,readFG_data_Array,receiverId_data_Array,referenceId_data_Array,referenceName_data_Array,reportFullPath_data_Array,reportName_data_Array,senderId_data_Array,senderName_data_Array,sentDate_data_Array,subject_data_Array; 
@synthesize detailViewController;
@synthesize window = _window;
@synthesize navigationController;
@synthesize delegate;
@synthesize unReadCount;
@synthesize webData;

-(void)doParse{

    NSLog(@"doParse"); //debug

    [myloxfileArray removeAllObjects];

    NSString *soapMessage = [NSString stringWithFormat:
                             @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                             "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:con=\"http://controller.exportingreportwebservice.lox.org/\">\n"
                             "<soapenv:Header/>\n"
                             "<soapenv:Body>\n"
                             "<con:getReport>\n"
                             "<arg0>%@</arg0>\n"
                             "</con:getReport>\n"
                             "</soapenv:Body>\n"
                             //"</soapenv:Envelope>\n", nameInput.text
                             "</soapenv:Envelope>\n", @"2711"
                             ];
    //NSLog(soapMessage);

    NSURL *url = [NSURL URLWithString:@"http://iloxley.loxley.co.th:8081/ExportingReportWebservice/ExportingReportWebservice?wsdl"];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];

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

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

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

        NSLog(@"theConnection is NULL");
    }

    NSLog(@"end doParse"); //debug

}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"connection didReceiveResponse"); //debug
    [webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSLog(@"connection didReceiveData"); //debug
    [webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"ERROR with theConenction");
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"connectionDidFinishLoading"); //debug
    NSLog(@"DONE. Received Bytes: %d", [webData length]);
    NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
    //NSLog(@"theXML = %@ ", theXML);
    //NSLog(@"webData = %@ ", webData);
    //NSXMLParser *nsXmlParser = [[NSXMLParser alloc] initWithContentsOfURL:theXML];
    NSXMLParser *nsXmlParser = [[NSXMLParser alloc] initWithData: webData];

    // create and init our delegate
    xml_myloxfile_Parser *parser = [[xml_myloxfile_Parser alloc] initXMLParser];

    //set delegate
    [nsXmlParser setDelegate:parser];

    //parsing...
    BOOL success = [nsXmlParser parse];

    if (success) {
        NSLog(@"if success"); //debug

        exportId_data_Array = [parser getExportId_data_Array];
        readFG_data_Array = [parser getReadFG_data_Array];
        receiverId_data_Array = [parser getReceiverId_data_Array];
        referenceId_data_Array = [parser getReferenceId_data_Array];
        referenceName_data_Array = [parser getReferenceName_data_Array];
        reportFullPath_data_Array = [parser getReportFullPath_data_Array];
        reportName_data_Array = [parser getReportName_data_Array];
        senderId_data_Array = [parser getSenderId_data_Array];
        senderName_data_Array = [parser getSenderName_data_Array];
        sentDate_data_Array = [parser getSentDate_data_Array];
        subject_data_Array = [parser getSubject_data_Array];

        items_exportId = exportId_data_Array;
        items_readFG = readFG_data_Array;
        items_receiverId = receiverId_data_Array;
        items_referenceId = referenceId_data_Array;
        items_referenceName = referenceName_data_Array;
        items_reportFullPath = reportFullPath_data_Array;
        items_reportName = reportName_data_Array;
        items_senderId = senderId_data_Array;
        items_senderName = senderName_data_Array;
        items_sentDate = sentDate_data_Array;
        items_subject = subject_data_Array;

        NSLog(@"items_subject = %@ ", items_subject);


    } else {
        NSLog(@"fail");
    }

}

-(void) loadView
{
    NSLog(@"loadView"); //debug
    if (items_subject == nil) {
        //items_subject = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",nil];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSLog(@"numberOfSectionsInTableView"); //debug
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"tableView numberOfRowsInSection"); //debug
    // Return the number of rows in the section.
    NSLog(@"[items_subject count] = %d ", [items_subject count]);
    return [items_subject count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"tableView cellForRowAtIndexPath"); //debug
    static NSString *CellIdentifier = @"Cell";

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

    // Configure the cell...
    cell.textLabel.text = [NSString stringWithFormat:@"%@" ,[items_subject objectAtIndex:indexPath.row]];

    //  Populate the cell's detail text label.
    NSString *detailText = [NSString stringWithFormat:@"date: %@  from: %@  reference: %@",
                            [[items_sentDate objectAtIndex:indexPath.row] lowercaseString],[[items_senderName objectAtIndex:indexPath.row] lowercaseString],[[items_referenceName objectAtIndex:indexPath.row] lowercaseString]];


    [[cell detailTextLabel] setText:detailText];

    //CGAffineTransform translate = CGAffineTransformMakeTranslation(10.0, 0.0); 
    //[[cell detailTextLabel] setTransform:translate];

    //unReadCount = 0;

    if([[items_readFG objectAtIndex:indexPath.row] isEqualToString:@"N"]){
        cell.textLabel.textColor = [UIColor redColor];
        unReadCount = unReadCount+1;
    }else{
        cell.textLabel.textColor = [UIColor blackColor];
        unReadCount = unReadCount;
    }

    NSLog(@"unReadCount = %d ",unReadCount);

    //Adding A Badge Number
    //[[UIApplication sharedApplication] setApplicationIconBadgeNumber:unReadCount];

    return cell;
}

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

    NSLog(@"tableView didSelectRowAtIndexPath"); //debug

    //sof2
    DetailViewController *detailController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil];
    [detailController changeExportIdText:[exportId_data_Array objectAtIndex:indexPath.row]];
    [detailController changeReadFGText:[readFG_data_Array objectAtIndex:indexPath.row]];
    [detailController changeReceiverIdText:[receiverId_data_Array objectAtIndex:indexPath.row]];
    [detailController changeReferenceIdText:[referenceId_data_Array objectAtIndex:indexPath.row]];
    [detailController changeReferenceNameText:[referenceName_data_Array objectAtIndex:indexPath.row]];
    [detailController changeReportFullPathText:[reportFullPath_data_Array objectAtIndex:indexPath.row]];
    [detailController changeReportNameText:[reportName_data_Array objectAtIndex:indexPath.row]];
    [detailController changeSenderIdText:[senderId_data_Array objectAtIndex:indexPath.row]];
    [detailController changeSenderNameText:[senderName_data_Array objectAtIndex:indexPath.row]];
    [detailController changeSentDateText:[sentDate_data_Array objectAtIndex:indexPath.row]];
    [detailController changeSubjectText:[subject_data_Array objectAtIndex:indexPath.row]];

    //[tableView deselectRowAtIndexPath:indexPath animated:YES];

    if([delegate respondsToSelector:@selector(showItemDetails:)])
    {
        //[delegate showItemDetails:[subject_data_Array objectAtIndex:indexPath.row]];
        [delegate showItemDetails:[NSString stringWithFormat:@"%@||%@||%@||%@||%@||%@||%@||%@||%@||%@||%@"
                                   ,[exportId_data_Array objectAtIndex:indexPath.row]
                                   ,[readFG_data_Array objectAtIndex:indexPath.row]
                                   ,[receiverId_data_Array objectAtIndex:indexPath.row]
                                   ,[referenceId_data_Array objectAtIndex:indexPath.row]
                                   ,[referenceName_data_Array objectAtIndex:indexPath.row]
                                   ,[reportFullPath_data_Array objectAtIndex:indexPath.row]
                                   ,[reportName_data_Array objectAtIndex:indexPath.row]
                                   ,[senderId_data_Array objectAtIndex:indexPath.row]
                                   ,[senderName_data_Array objectAtIndex:indexPath.row]
                                   ,[sentDate_data_Array objectAtIndex:indexPath.row]
                                   ,[subject_data_Array objectAtIndex:indexPath.row]
                                   ]];//test multi var'
    }

    // Alert will pop up when using Simulator or iPod Touch for example
    /*
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[note_subject_data_Array objectAtIndex:indexPath.row] message:@"Test" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];
     [alert show];
     */
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {

    return UITableViewCellEditingStyleDelete;

}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (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.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    NSLog(@"viewDidLoad"); //debug

    [self doParse];

    [super viewDidLoad];
}

- (void)viewDidUnload
{
    NSLog(@"viewDidUnload"); //debug

    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}

@end

日志:

2012-09-11 15:27:15.377 MyLoxFile[2698:f803] loadView
2012-09-11 15:27:15.378 MyLoxFile[2698:f803] viewDidLoad
2012-09-11 15:27:15.379 MyLoxFile[2698:f803] doParse
2012-09-11 15:27:15.382 MyLoxFile[2698:f803] if theConnection
2012-09-11 15:27:15.382 MyLoxFile[2698:f803] end doParse
2012-09-11 15:27:15.385 MyLoxFile[2698:f803] numberOfSectionsInTableView
2012-09-11 15:27:15.386 MyLoxFile[2698:f803] tableView numberOfRowsInSection
2012-09-11 15:27:15.391 MyLoxFile[2698:f803] [items_subject count] = 0 
2012-09-11 15:27:15.454 MyLoxFile[2698:f803] connection didReceiveResponse
2012-09-11 15:27:15.455 MyLoxFile[2698:f803] connection didReceiveData
2012-09-11 15:27:15.455 MyLoxFile[2698:f803] connectionDidFinishLoading
2012-09-11 15:27:15.455 MyLoxFile[2698:f803] DONE. Received Bytes: 1948
2012-09-11 15:27:15.457 MyLoxFile[2698:f803] if success
2012-09-11 15:27:15.457 MyLoxFile[2698:f803] items_subject = (
    "Surgical Report ",
    "Genotype Report",
    "HPV Report"
) 

它包含数组,但没有tableView numberOfRowsInSection

在此代码之前,我使用XML(而不是WSDL)它的工作

这是我的代码 - &gt; http://www.vasuta.com/ios/MyLoxFile.zip

3 个答案:

答案 0 :(得分:0)

更新NSArray后是否使用过[uitableview reloadData]?

答案 1 :(得分:0)

在您的NSURLConnection完成下载之前,似乎您的tableview是load并调用其委托方法。 您已通过

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

所以连接是异步的。在tableview加载其视图之前或之后,您无法确定它是否完成。 但是您可以随时检查tableview方法以使您的应用程序正常工作。例如:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    int nbCount = [items_subject count];
    if (nbCount == 0)
        return 1;
    else 
        return [items_subject count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    int nbCount = [items_subject count];
    static NSString *CellIdentifier = @"Cell";

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

    // Configure the cell...
    if (nbCount ==0)
        cell.textLabel.text == @"Loading";
    else
    {
        // your configuration
        cell.textLabel.text = [NSString stringWithFormat:@"%@" ,[items_subject objectAtIndex:indexPath.row]];
    }
    return cell;
 }

在您的方法“ConnectionDidFinishLoading”中,您可以调用[tableview reloaddata]来更新您的tableview。

[编辑]于12/09/2012

我的代码工作正常(不是一个很好的解决方案),你应该重新思考如何组织你的viewControllers。

  1. 将IBOutlet TableView添加到myloxfileController.tableView。 在界面myloxfileContoller.tableView = myloxfileTable;的{​​{1}}中的myloxfileContoller.view = myloxfileContoller.tableView;行之后添加此行- (void)viewDidLoad

  2. 连接完成后重新加载tableview数据。 在界面ViewController的{​​{1}}中的[self.tableView reloadData];行之后添加此行items_subject = subject_data_Array;

  3. 这将有效,因为下载完成后,您将重新加载IBOutlet TableView的数据。

答案 2 :(得分:0)

替换为

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"tableView numberOfRowsInSection"); //debug
    // Return the number of rows in the section.
    NSLog(@"[items_subject count] = %@ ", [items_subject count]);
    return [items_subject count];
}