2009-11-12 84 views
0

我正在訪問一個Web服務,其中我給出了開始日期和結束日期,並作爲回報,我從Web服務中獲取了一個字符串數組。 字符串數組中的每個字符串均採用此格式 「1 |銀行名稱|帳戶NO | 121 |抽屜名稱」。現在我想在表格視圖的第一行顯示這個內容。 第二行應該被String數組的第二個字符串佔用。 我嘗試了下面的方式,但我的表似乎是空的。請幫助。在表視圖中顯示數組的內容

#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]; 
      [email protected]""; 
      [email protected]""; 

     }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:(UITableView *)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 

回答

0

解析器完成後,您需要[tableView reloadData]

+0

我試過,但應用程序崩潰。在控制檯中沒有錯誤 – shreedevi 2009-11-12 17:26:12

+0

爲您的代碼添加一些斷點,特別是在您的UITableView委託方法和reloadData中。瀏覽你的代碼,看看你能否找到崩潰的根源。最後,在找出位置後,查找具有不正確值的變量。 – 2009-11-12 18:19:15

0

它可能與你調用解析器的地方有關。如果解析器沒有運行,那麼表格將是空白的。有數據時,您不會顯示強制更新表的代碼。它可能'tableView:numberOfRowsInSection:'是根本不會在視圖的初始加載後調用。

我建議在解析器的末尾記錄chucks以確保它實際上具有值。您也可以在填充單元格之前記錄它。

我建議將解析器和連接到Web服務的方法移動到與表控制器不同的對象中。一般來說,你想分離控制器和視圖的數據的抓取/格式化/管理。在這種特殊情況下,當需要更新表格時,您不希望控制器等待某些回覆。相反,你應該有一個模型對象,只需將控制器交給一個數組。這也可以很容易地處理與提取數據有關的錯誤。