提问人:Marking 提问时间:3/7/2017 更新时间:3/7/2017 访问量:57
XML 解析数据没有从 viewController 到 tableViewController 在目标 c 中?
XML Parsing data not getting from viewController to tableViewController in objective c?
问:
我成功地从服务器获取了xml数据,但我无法将该数据从viewController正确地发送到tableViewController。我到底想做什么发送数据,这是我的代码......
在 viewController 中...
- (IBAction)parserButtonClicked:(UIButton *)sender {
[self.appTitleArray removeAllObjects];
[self.releaseDateArray removeAllObjects];
[self.summeryArray removeAllObjects];
[self.priceArray removeAllObjects];
[self.imageArray removeAllObjects];
DisplayAppsTableViewController *datvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DATVC"];
if ((self.countrySegmentControl.selectedSegmentIndex == 0 || self.countrySegmentControl.selectedSegmentIndex == 1 || self.countrySegmentControl.selectedSegmentIndex == 2) && (self.categorySegmentControl.selectedSegmentIndex == 0 || self.categorySegmentControl.selectedSegmentIndex == 1) && (self.countSegmentControl.selectedSegmentIndex == 0 || self.countSegmentControl.selectedSegmentIndex == 1 || self.countSegmentControl.selectedSegmentIndex == 2 || self.countSegmentControl.selectedSegmentIndex == 3)) {
[self stringManipulation];
//This is the link rom stringManipulation method
// https://itunes.apple.com/in/rss/topfreeapplications/limit=10/xml
// https://itunes.apple.com/in/rss/toppaidapplications/limit=10/xml
self.URLRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.XMLurl]];
self.URLSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
self.dataTask = [self.URLSession dataTaskWithRequest:self.URLRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
self.XMLParser = [[NSXMLParser alloc]initWithData:data];
self.XMLParser.delegate = self;
BOOL success = [self.XMLParser parse];
if (success) {
NSLog(@"Parse success");
} else {
NSLog(@"Parse not success");
}
}];
[self.dataTask resume];
}
datvc.appTitleArray1 = self.appTitleArray;
datvc.releaseDateArray1 = self.releaseDateArray;
datvc.priceArray1 = self.priceArray;
datvc.summeryArray1 = self.summeryArray;
datvc.imageArray1 = self.imageArray;
NSLog(@"Here it's NOT printing data successfully : %@", datvc.appTitleArray1);
[self.navigationController pushViewController:datvc animated:YES];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(NSDictionary<NSString *, NSString *> *)attributeDict {
self.element = elementName;
if([elementName isEqualToString:@"im:releaseDate"]) {
NSString *date=[attributeDict objectForKey:@"label"];
[self.releaseDateArray addObject:date];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
NSString *trimmedString = [string stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (trimmedString.length > 0) {
if ([self.element isEqualToString:@"im:name"]) {
[self.appTitleArray addObject:string];
}
if ([self.element isEqualToString:@"summary"]) {
[self.summeryArray addObject:string];
}
if ([self.element isEqualToString:@"im:price"]) {
[self.priceArray addObject:string];
}
if ([self.element isEqualToString:@"im:image"]) {
[self.imageArray addObject:string];
}
}
NSLog(@"Here it's parsing data successfully : %lu", [self.appTitleArray count]);
}
在 tableViewController 中
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"Not getting data from viewController : %lu", [self.releaseDateArray1 count]);
return [self.releaseDateArray1 count];
}
答:
1赞
Matloob Hasnain
3/7/2017
#1
if (success)
{
NSLog(@"Parse success");
datvc.appTitleArray1 = self.appTitleArray;
datvc.releaseDateArray1 = self.releaseDateArray;
datvc.priceArray1 = self.priceArray;
datvc.summeryArray1 = self.summeryArray;
datvc.imageArray1 = self.imageArray;
NSLog(@"Here it's NOT printing data successfully : %@", datvc.appTitleArray1);
[self.navigationController pushViewController:datvc animated:YES];
}
else
{
NSLog(@"Parse not success");
}
请尝试这样做,这是因为您在解析之前设置了数据。
评论
0赞
Matloob Hasnain
3/7/2017
它会成功吗?
0赞
Marking
3/7/2017
它立即从服务器获取数据,但仅需要一些时间进行导航
1赞
Anbu.Karthik
3/7/2017
#2
不像
DisplayAppsTableViewController *datvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DATVC"];
if ((self.countrySegmentControl.selectedSegmentIndex == 0 || self.countrySegmentControl.selectedSegmentIndex == 1 || self.countrySegmentControl.selectedSegmentIndex == 2) && (self.categorySegmentControl.selectedSegmentIndex == 0 || self.categorySegmentControl.selectedSegmentIndex == 1) && (self.countSegmentControl.selectedSegmentIndex == 0 || self.countSegmentControl.selectedSegmentIndex == 1 || self.countSegmentControl.selectedSegmentIndex == 2 || self.countSegmentControl.selectedSegmentIndex == 3)) {
[self stringManipulation];
//This is the link rom stringManipulation method
// https://itunes.apple.com/in/rss/topfreeapplications/limit=10/xml
// https://itunes.apple.com/in/rss/toppaidapplications/limit=10/xml
self.URLRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.XMLurl]];
self.URLSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
self.dataTask = [self.URLSession dataTaskWithRequest:self.URLRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
self.XMLParser = [[NSXMLParser alloc]initWithData:data];
self.XMLParser.delegate = self;
BOOL success = [self.XMLParser parse];
if (success) {
NSLog(@"Parse success");
} else {
NSLog(@"Parse not success");
}
}];
[self.dataTask resume];
}
datvc.appTitleArray1 = self.appTitleArray;
datvc.releaseDateArray1 = self.releaseDateArray;
datvc.priceArray1 = self.priceArray;
datvc.summeryArray1 = self.summeryArray;
datvc.imageArray1 = self.imageArray;
NSLog(@"Here it's NOT printing data successfully : %@", datvc.appTitleArray1);
[self.navigationController pushViewController:datvc animated:YES];
做赞
原因:你被称为附加线程,在它会需要一些额外的时间来执行,执行成功后你需要导航。
if ((self.countrySegmentControl.selectedSegmentIndex == 0 || self.countrySegmentControl.selectedSegmentIndex == 1 || self.countrySegmentControl.selectedSegmentIndex == 2) && (self.categorySegmentControl.selectedSegmentIndex == 0 || self.categorySegmentControl.selectedSegmentIndex == 1) && (self.countSegmentControl.selectedSegmentIndex == 0 || self.countSegmentControl.selectedSegmentIndex == 1 || self.countSegmentControl.selectedSegmentIndex == 2 || self.countSegmentControl.selectedSegmentIndex == 3)) {
[self stringManipulation];
//This is the link rom stringManipulation method
// https://itunes.apple.com/in/rss/topfreeapplications/limit=10/xml
// https://itunes.apple.com/in/rss/toppaidapplications/limit=10/xml
self.URLRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.XMLurl]];
self.URLSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
self.dataTask = [self.URLSession dataTaskWithRequest:self.URLRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
self.XMLParser = [[NSXMLParser alloc]initWithData:data];
self.XMLParser.delegate = self;
BOOL success = [self.XMLParser parse];
if (success) {
NSLog(@"Parse success");
dispatch_sync(dispatch_get_main_queue(), ^{
// Update the UI on the main thread.
DisplayAppsTableViewController *datvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DATVC"];
datvc.appTitleArray1 = self.appTitleArray;
datvc.releaseDateArray1 = self.releaseDateArray;
datvc.priceArray1 = self.priceArray;
datvc.summeryArray1 = self.summeryArray;
datvc.imageArray1 = self.imageArray;
NSLog(@"Here it's NOT printing data successfully : %@", datvc.appTitleArray1);
[self.navigationController pushViewController:datvc animated:YES];
});
} else {
NSLog(@"Parse not success");
}
}];
[self.dataTask resume];
}
评论
0赞
Marking
3/7/2017
是的,它工作成功,但它正在立即导航
0赞
Anbu.Karthik
3/7/2017
@VadlaniMarking - 数据是否正在接收,否则您需要任何其他数据
0赞
Anbu.Karthik
3/7/2017
@VadlaniMarking - 好吧,您需要面临的问题,与此相关的任何替代想法,或者您需要什么样的输出
0赞
Anbu.Karthik
3/7/2017
@VadlaniMarking - 等待
0赞
Marking
3/7/2017
我想将获取的数据打印到 tableViewController 中
评论