首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来自服务器的数据不显示在tableview中。

来自服务器的数据不显示在tableview中。
EN

Stack Overflow用户
提问于 2016-11-28 12:50:06
回答 1查看 53关注 0票数 1

我有两个屏幕,在第一个点击按钮时,它应该转到第二个屏幕,从服务器获取数据,在第二个screen.but中显示在表视图中,代码如下,我无法在表视图中显示数据

代码语言:javascript
复制
#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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-28 12:56:36

做两件事

First

代码语言:javascript
复制
- (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];

}

第二

代码语言:javascript
复制
 - (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=@"";
}

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

  }

 }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40844804

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档