1

我正在尝试创建一个简单的 RSS 阅读器,我想description从 rss 提要中获取“”标签并将其显示在我的应用程序中,有什么帮助吗?

是否建议使用NSXMLParser或任何其他解析器来简化我的工作?

我使用的源代码来自:iOS 编程 RSS 阅读器教程

4

1 回答 1

1

该教程还剩下什么。一切正常。他们只是离开使用描述项。正确的 ...?

这是该教程的内容:

@interface ViewController (){

 NSXMLParser *parser;
    NSMutableArray *feeds;
    NSMutableDictionary *item;
    NSMutableString *title;
    NSMutableString *link;
    NSString *element;

 NSMutableString *desc; // Description .
}

只需粘贴此代码。像魅力一样工作:

#pragma mark - parsing of RssFeed Values

-(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];
        desc = [[NSMutableString alloc] init];  
    }
}

-(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:desc forKey:@"description"];
        [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"]){
        [desc appendString:string];
    }
}


-(void) parserDidEndDocument:(NSXMLParser *)parser{
    [self.tableView reloadData];
}

在任何地方使用此参数Desc来获取 RSSFeed 项目的描述。

这是它的完成:

-(void) tableView: (UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     NSString *string = [feeds[indexPath.row] objectForKey: @"description"];

    stringUrl = [feeds[indexPath.row] objectForKey:@"link"];
     NSLog(@"Description %@", string);

   actionSheet = [[UIActionSheet alloc] initWithTitle:string delegate:self cancelButtonTitle:Nil destructiveButtonTitle:@"OK" otherButtonTitles:@"GoTo URL", nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;

    [actionSheet showInView:self.view];

}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
{
  self.actionSheet.delegate = self;
    switch (buttonIndex) {
        case 0:
              NSLog(@"ButtonIndex at 0");
                break;

        case 1:
             NSLog(@"ButtonIndex at 1");
           //Add your Segue functionalities over here to open link on the browser .

    }
                break;
        } 

}

如果您在这里有任何问题,请让我知道:::

于 2013-07-23T05:06:02.283 回答