2

Newbie to Xcode and would be thrilled to get an answer to this question. I am trying to get the image url tag from a rss feed and add it to my custom table view cell. The other text I get fine.

This is the rss row:

<Item>
<title>this is a title</title>
<description>this is a description</description>
<link>http://www.something.com</link>
<pubDate>Fri, 09 Aug 2013</pubDate>
<enclosure type="image/jpg" url="http://www.ThisIsTheUrlIWant.com" />
</item>

This is part of my code without any image logic:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath {
CustomCellNews *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"  forIndexPath:indexPath];
cell.titleLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"title"];
cell.descriptionLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"description"];
//set cell image here
return cell;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:    (NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {

element = elementName;

if ([element isEqualToString:@"item"]) {

    item    = [[NSMutableDictionary alloc] init];
    title   = [[NSMutableString alloc] init];
    link    = [[NSMutableString alloc] init];
    description = [[NSMutableString alloc] init];
    //image stuff
}
}

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

if ([elementName isEqualToString:@"item"]) {

    [item setObject:title forKey:@"title"];
    [item setObject:link forKey:@"link"];
    [item setObject:description forKey:@"description"];
    //image stuff

    [feeds addObject:[item copy]];

}

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

if ([element isEqualToString:@"title"]) {
    [title appendString:string];
} else if ([element isEqualToString:@"link"]) {
    [link appendString:string];
} else if ([element isEqualToString:@"description"]) {
    [ingress appendString:string];
}
//image stuff

}

Thanks

Community
  • 1
  • 1
TommyF
  • 381
  • 1
  • 7
  • 22

2 Answers2

1

Try this:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ([elementName isEqualToString:@"enclosure"]) {
        NSString *units = [attributeDict objectForKey:@"url"];
    }
}

Hope this helps.

Vish
  • 341
  • 1
  • 4
  • 19
  • But this will only add the url to the units variable and not to a specific item row? I would like it to be added to the feeds array in the didEndElement. That way I can get it in the cellForRowAtIndexPath via the feeds. Any way to do that? – TommyF Aug 14 '13 at 18:02
  • Ok in that way. Ya so you can put up your above code in this method and then add up the units object in your array after your item object has been inserted. Clear! You try or if its not working then I write up code for you. – Vish Aug 15 '13 at 09:46
1

First get the image URL from the attributes dictionary in the enclosure element inside the didStartElement method:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
//NSLog(@"Main View: didStartElement: %@\tstart", elementName);

element = elementName;

if ([element isEqualToString:@"item"]) {

    item        =       [[NSMutableDictionary alloc] init];
    title       =       [[NSMutableAttributedString alloc] init];
    description =       [[NSMutableAttributedString alloc] init];
    link        =       [[NSMutableString alloc] init];

}


if ([element isEqualToString:@"enclosure"]) {
    imageType   =   [attributeDict objectForKey:@"type"];
    imageUrl    =   [attributeDict objectForKey:@"url"];

}
//NSLog(@"RSS Utility: didStartElement: %@", elementName);

}

Then add imageUrl to your item array inside the didEndElement method:

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

if ([elementName isEqualToString:@"item"]) {

    [item setObject:title forKey:@"title"];
    [item setObject:link forKey:@"link"];
    [item setObject:description forKey:@"description"];
    [item setObject:imageType forKey:@"imageType"];
    [item setObject:imageUrl forKey:@"imageUrl"];
    [_feeds addObject:[item copy]];

}

}

This may not work for all rss feeds. I recommend you put NSLog statements inside the didStartElement to understand where the image url can be found:

NSLog(@"\nXML Parser:didStartElement:%@\n\t\t\tnameSpaceURI:%@\n\t\t\tqualifiedName:%@\n\t\t\tattributes:%@", elementName, namespaceURI, qName, attributeDict);
hvvc
  • 31
  • 2