-3

为什么我的程序在NSURLConnectiontableView 之前不起作用?

//  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)它的工作

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

4

3 回答 3

0

replace with

- (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];
}
于 2012-09-11T12:18:14.307 回答
0

你在更新你的 NSArray 后使用过 [uitableview reloadData] 吗?

于 2012-09-11T08:52:53.020 回答
0

在您的 NSURLConnection 完成下载之前,您的 tableview 似乎已加载并调用其委托方法。你已经启动了 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。

[编辑] 2012 年 9 月 12 日

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

  1. 将 IBOutlet TableView 添加到 myloxfileController.tableView。在 interfacemyloxfileContoller.tableView = myloxfileTable;的 line myloxfileContoller.view = myloxfileContoller.tableView;in之后添加这一行。- (void)viewDidLoadViewController

  2. 连接完成后重新加载 tableview 数据。在 interface[self.tableView reloadData];的 line items_subject = subject_data_Array;in之后添加这一行。-(void)connectionDidFinishLoading:(NSURLConnection *)connectionMyLoxFileTVController

这将起作用,因为下载完成后,您会重新加载 IBOutlet TableView 的数据。

于 2012-09-11T08:57:06.120 回答