gpt4 book ai didi

ios - XMLParsing 不在 TableView 上显示数据

转载 作者:行者123 更新时间:2023-11-29 13:07:30 25 4
gpt4 key购买 nike

看来我无法在 xml 中的表格中显示数据...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

dataFileHolder *currentData = [[xmlParser listPopulated] objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

if(cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

}

CGRect nameFrame = CGRectMake(45, 2, 265, 30);
UILabel *nameLabel = [[UILabel alloc] initWithFrame:nameFrame];
nameLabel.numberOfLines = 2;
nameLabel.font = [UIFont boldSystemFontOfSize:12];
nameLabel.text = [currentData nameOfCat];
[cell.contentView addSubview:nameLabel];

CGRect descFrame = CGRectMake(45, 40, 265, 10);
UILabel *descLabel = [[UILabel alloc] initWithFrame:descFrame];
descLabel.font = [UIFont systemFontOfSize:10];
descLabel.text = [currentData descriptionOfCat];
[cell.contentView addSubview:descLabel];
return cell;

}

这是 .m 文件中的 xmlparser 类

- (id) loadXMLByURL :(NSString *) urlString {

listPopulated = [[NSMutableArray alloc]init];
NSURL *url = [NSURL URLWithString:urlString];
NSData *data= [[NSData alloc]initWithContentsOfURL:url];
parser = [[NSXMLParser alloc]initWithData:data];
parser.delegate = self;

return self;

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

}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:@"category"]){
dataCurrent = [dataFileHolder alloc];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

if([elementName isEqualToString:@"name"]){
dataCurrent.nameOfCat = currentList;
}
if ([elementName isEqualToString:@"description"]){
dataCurrent.descriptionOfCat = currentList;
}
if ([elementName isEqualToString:@"category"]) {
[listPopulated addObject:dataCurrent];
dataCurrent = nil;
currentList = nil;
}
}

然后我有一个类将保存从 xml 解析的数据。 .m 数据持有者是这个

@implementation dataFileHolder

@synthesize nameOfCat = _nameOfCat;
@synthesize descriptionOfCat = _descriptionOfCat;

@end

这是xml数据

category>
<cat_id>59</cat_id>
<name>Foods & Meals</name>
<description>
iRabwah Foods & Meals service provides you pizza packages, burger meals and spicy rice meal. Give a best meal to your loved ones in rabwah by iRabwah
</description>
<image>
http://skylitecommunication.ipower.com/--irabwha-opc/image/data/1287455832_monotone_fork_spoon_eat_launch_restaurant_dinner.png
</image>
<sub_cat>
<sub_id>67</sub_id>
<sub_name>Spicy Rice Meal</sub_name>
<sub_desc>
iRabwah offers you to give a treat to your loved ones by spicy rice meal, best taste and special rice to your loved ones in rabwah.
</sub_desc>
<sub_image>
http://skylitecommunication.ipower.com/--irabwha-opc/image/data/hyderabadi_biryani.png
</sub_image>
</sub_cat>
<sub_cat>
<sub_id>69</sub_id>
<sub_name>Pizza Packages</sub_name>
<sub_desc>
Our pizzas have exciting, universal flavors made in Rabwah from freshly baked pizza dough including pepperoni, oregano, sauces and fresh ingredients.
</sub_desc>
<sub_image>
http://skylitecommunication.ipower.com/--irabwha-opc/image/data/pizza.png
</sub_image>
</sub_cat>
<sub_cat>
<sub_id>68</sub_id>
<sub_name>Burger Meals</sub_name>
<sub_desc>
iRabwah provides you a best way to dedicate a wonderful meal to your loves ones in rabwah by offering burger meals with fries coke and pizza.
</sub_desc>
<sub_image>
http://skylitecommunication.ipower.com/--irabwha-opc/image/data/burger-clip-art-4.png
</sub_image>
</sub_cat>
</category>

请告诉我我缺少什么

最佳答案

您必须有一个 BOOL 变量才能开始捕获字符。

或者这样尝试:

- (id) loadXMLByURL :(NSString *) urlString {

listPopulated = [[NSMutableArray alloc]init];
NSURL *url = [NSURL URLWithString:urlString];
NSData *data= [[NSData alloc]initWithContentsOfURL:url];
parser = [[NSXMLParser alloc]initWithData:data];
parser.delegate = self;

[parser parse];

return self;
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
currentList = [[NSMutableString alloc] initWithString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];

}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:@"category"]){
dataCurrent = [dataFileHolder alloc];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

if([elementName isEqualToString:@"name"]){
dataCurrent.nameOfCat = currentList;
}
if ([elementName isEqualToString:@"description"]){
dataCurrent.descriptionOfCat = currentList;
}
if ([elementName isEqualToString:@"category"]) {
[listPopulated addObject:dataCurrent];
dataCurrent = nil;
currentList = nil;
}
}

关于ios - XMLParsing 不在 TableView 上显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18251668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com