1

使用 NSXMLParser 我可以从本地 XML 文件中检索数据,但我在 XML 文件中给出的图像路径没有检索到。下面的代码向您详细展示了。

NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Prices.xml"];
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
egsParser *theParser =[[egsParser alloc] initParser];
[xmlParser setDelegate:theParser];

从这段代码中,我可以解析数据,但不知道如何检索图像并将其显示在表格视图中。表格视图代码如下。

- (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];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
 }


theLists = [app.listArray objectAtIndex:indexPath.row];
cell.textLabel.text = theLists.title;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}

请提出一个想法,从本地 XML 文件解析和显示表格视图中的图像

4

2 回答 2

1

使用以下代码

 cell.imageView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"Your Image Link"]]];

or 

 cell.imageView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSString stringWithFormat:@"Your Image Link"]]];

根据您的代码,它将如下所示。

 cell.imageView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:theList.imgUrl]]];

如果你想从保存的路径中检索图像

 cell.imageView.image= = [UIImage imageWithContentsOfFile:@"yourSavedpath"];
于 2012-11-22T05:13:29.787 回答
0

您必须使用委托方法来解析 XML 文件...

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
}
- (void)parserDidStartDocument:(NSXMLParser *)parser {
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
}

通过使用这些委托方法,您可以获得所需的元素。

于 2012-11-22T05:09:40.153 回答