2

Xcode の初心者であり、この質問に対する回答を得ることができれば幸いです。RSS フィードから画像 URL タグを取得して、カスタム テーブル ビュー セルに追加しようとしています。他のテキストは大丈夫です。

これはRSS行です:

<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>

これは、イメージ ロジックのないコードの一部です。

- (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

}

ありがとう

4

2 に答える 2

1

これを試して:

- (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"];
    }
}

お役に立てれば。

于 2013-08-14T12:35:02.030 に答える
1

didStartElementまず、メソッド内のエンクロージャ要素の属性ディクショナリから画像 URL を取得します。

- (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);

}

次に、メソッド内の配列に追加imageUrlします。itemdidEndElement

- (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]];

}

}

これは、すべての RSS フィードで機能するとは限りません。画像の URL がどこにあるかを理解するために、NSLogステートメントを の中に入れることをお勧めします。didStartElement

NSLog(@"\nXML Parser:didStartElement:%@\n\t\t\tnameSpaceURI:%@\n\t\t\tqualifiedName:%@\n\t\t\tattributes:%@", elementName, namespaceURI, qName, attributeDict);
于 2014-05-06T05:22:11.437 に答える