2016-11-28 73 views
1

我有2个屏幕,在第一次当我点击一个按钮时,它应该到第2屏幕从服务器获取数据并在第2屏幕的表格视图中显示。但使用以下代码我无法在表视图中显示的数据来自服务器的数据没有被显示在桌面视图中

#import "FirstTableViewController.h" 

@interface FirstTableViewController()<NSXMLParserDelegate> 
@property(nonatomic,strong)NSXMLParser*xmlparse; 
@property(nonatomic,strong)NSMutableString*tempstr; 
@property(nonatomic,strong)NSMutableArray*arr; 

@end 

@implementation FirstTableViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    NSMutableURLRequest*req=[[NSMutableURLRequest alloc]init]; 
    NSURL*url=[NSURL URLWithString:@"http://www.thomas-bayer.com/sqlrest/CUSTOMER/"]; 
    [req setURL:url]; 
    [[[NSURLSession sharedSession]dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { 
     // SMXMLDocument *document = [SMXMLDocument documentWithData:data error:&error]; 

     NSString*str=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; 
     _xmlparse=[[NSXMLParser alloc]initWithData:data]; 
     _xmlparse.delegate=self; 
     [_xmlparse parse]; 
    }] resume]; 
    [_table reloadData]; 
} 

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ 
    _tempstr=[[NSMutableString alloc]initWithString:string]; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(NSDictionary<NSString *, NSString *> *)attributeDict;{ 
    if([elementName isEqualToString:@"CUSTOMER"]){ 
     self.tempstr=[[NSMutableString alloc]init]; 

    } 
    if([elementName isEqualToString:@"CUSTOMERList"]){ 
     self.arr=[[NSMutableArray alloc]init]; 

    } 

} 
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName;{ 

    if([elementName isEqualToString:@"CUSTOMER"]){ 
     [_arr addObject:self.tempstr]; 
     self.tempstr=nil; 
    } 
    // NSLog(@"arr is %@",self.arr); 
} 
#pragma mark - Table view data source 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return self.arr.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cel" forIndexPath:indexPath]; 

    cell.textLabel.text=[self.arr objectAtIndex:indexPath.row]; 
    return cell; 
} 

@end 

回答

1

做两件事情

首先

- (void)viewDidLoad { 
[super viewDidLoad]; 

arr = [NSMutableArray array]; // initilize the memory of array 

NSMutableURLRequest*req=[[NSMutableURLRequest alloc]init]; 
NSURL*url=[NSURL URLWithString:@"http://www.thomas-bayer.com/sqlrest/CUSTOMER/"]; 
[req setURL:url]; 
[[[NSURLSession sharedSession]dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { 
    // SMXMLDocument *document = [SMXMLDocument documentWithData:data error:&error]; 

    NSString*str=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; 
    _xmlparse=[[NSXMLParser alloc]initWithData:data]; 
    _xmlparse.delegate=self; 
    [_xmlparse parse]; 
}] resume]; 

} 

其次

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName;{ 

if([elementName isEqualToString:@"CUSTOMER"]){ 
    [_arr addObject:self.tempstr]; 
    self[email protected]""; 
} 

if(arr.count >0) 
{ 
    dispatch_async(dispatch_get_main_queue(), ^{ 
      [_table reloadData]; 
     }); 

    } 

} 
+0

兄弟...它不是我的代码段中的回流工作 – Nishanth

+0

....没有行的0 – Nishanth

+0

是你有'的输出arr' –

相关问题