2014-06-05 3 views
0

NSXMLParser를 사용하여 XML 파일을 구문 분석하므로 RSS 피드의 데이터를 테이블보기로 표시 할 수 있습니다. 그러나 제목 데이터는 올바르게 구문 분석되어 적절한 배열로 전달되지만 값을 셀 제목에 전달하면 모든 제목 뒤에 세 개의 마침표가 나타납니다. 셀에 전달되기 전에 콘솔에 값을 기록하고 텍스트 줄에 마침표가 없습니다. 또한 게시 날짜는 무엇이든지 상관없이 null을 반환합니다.NSXMLParser 문제

FeedContoller.m :

@interface FeedController2() 

@property (nonatomic, strong) NSArray* profileImages; 

@end 

@implementation FeedController2 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 


    titarry=[[NSMutableArray alloc] init]; 
    linkarray=[[NSMutableArray alloc] init]; 
    NSString *[email protected]"http://cazhigh.com/caz/gold/?feed=rss2"; 
    NSURL *url=[NSURL URLWithString:rssaddr]; 
    xmlparser =[[NSXMLParser alloc] initWithContentsOfURL:url]; 
    [xmlparser setDelegate:self]; 
    [xmlparser parse]; 

    NSString* boldFontName = @"GillSans-Bold"; 
    [self styleNavigationBarWithFontName:boldFontName]; 
    self.title = @"Blog Feed"; 

    self.feedTableView.dataSource = self; 
    self.feedTableView.delegate = self; 
    self.feedTableView.backgroundColor = [UIColor whiteColor]; 
    self.feedTableView.separatorColor = [UIColor colorWithWhite:0.9 alpha:0.6]; 
} 

/* 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 
*/ 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    //return [titarry count]; 
    if(titarry.count <= 5){ 
     return titarry.count; 
    }else{ 
     return 5; 
    } 
} 

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

    static NSString *CellIdentifier = @"FeedCell2"; 

    FeedCell2* cell = [tableView dequeueReusableCellWithIdentifier:@"FeedCell2"]; 

    if (cell == nil) { 
     cell = [[FeedCell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ; 
    } 

    cell.nameLabel.text=[titarry objectAtIndex:indexPath.row]; 
    NSLog(@"Title: %@", [titarry objectAtIndex:indexPath.row]); //Log title from array 
    NSLog(@"Date Posted: %@", [datearray objectAtIndex:indexPath.row]); //Log date posted 
    NSLog(@"Link Address: %@", [linkarray objectAtIndex:indexPath.row]); //Log link address 
    //cell.accessoryType=UITableViewCellSelectionStyleBlue; 
    return cell; 
} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 


    XMLViewController *second = [[XMLViewController alloc] initWithNibName:@"XMLViewController" bundle:nil]; 
    [self.navigationController pushViewController:second animated:YES]; 
    NSURL *url=[NSURL URLWithString:[linkarray objectAtIndex:indexPath.row]]; 
    NSURLRequest *req=[NSURLRequest requestWithURL:url]; 
    second.XMLWebView.scalesPageToFit=YES; 
    [second.XMLWebView loadRequest:req];//here we have to perform changes try to do some things here 

} 


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    return 200; 
} 

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

    classelement=elementName; 

    if([elementName isEqualToString:@"item"]) 
    { 
     itemselected=YES; 
     mutttitle=[[NSMutableString alloc] init]; 
     mutstrlink=[[NSMutableString alloc] init]; 
     mutstrdate=[[NSMutableString alloc] init]; 
    } 
} 

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName; 
{ 
    if([elementName isEqualToString:@"item"]) 
    { 
     itemselected=NO; 

     [titarry addObject:mutttitle]; 
     [linkarray addObject:mutstrlink]; 
     [datearray addObject:mutstrdate]; 

    } 

} 


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string; 
{ 
    if (itemselected) 
    { 
     if ([classelement isEqualToString:@"title"]) 
     { 
      [mutttitle appendString:string]; 
     } 
     else if ([classelement isEqualToString:@"link"]) 
     { 
      [mutstrlink appendString:string]; 
     }else if ([classelement isEqualToString:@"pubDate"]) 
     { 
      [mutstrdate appendString:string]; 
     } 
    } 
} 

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError; 
{ 
    UIAlertView *alt=[[UIAlertView alloc] initWithTitle:@"RSS Reader" 
               message:[NSString stringWithFormat:@"%@",parseError] 
               delegate:nil 
             cancelButtonTitle:@"Close" 
             otherButtonTitles:nil]; 

    [alt show]; 


} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

-(void)styleNavigationBarWithFontName:(NSString*)navigationTitleFont{ 

    UIImageView* searchView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"search.png"]]; 
    searchView.frame = CGRectMake(0, 0, 20, 20); 

    UIBarButtonItem* searchItem = [[UIBarButtonItem alloc] initWithCustomView:searchView]; 

    self.navigationItem.rightBarButtonItem = searchItem; 
} 

-(IBAction)dismissView:(id)sender{ 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

@end 

XML :

<item> 
<title>Back-to-school issue of Blue and Gold in the mail</title> 
<link>http://cazhigh.com/caz/gold/?p=2896</link> 
<comments>http://cazhigh.com/caz/gold/?p=2896#comments</comments> 
<pubDate>Thu, 29 Aug 2013 12:35:40 +0000</pubDate> 
<dc:creator>...</dc:creator> 
<category>...</category> 
<category>...</category> 
<category>...</category> 
<guid isPermaLink="false">http://cazhigh.com/caz/gold/?p=2896</guid> 
<description> 
<![CDATA[ 
<img width="150" height="150" src="http://cazhigh.com/caz/gold/wp-content/uploads/2013/08/BluGold_BacktoSchool2013_cover-150x150.jpg" class="attachment-thumbnail wp-post-image" alt="Blue and Gold (Back to School 2013)" style="display: block; margin-bottom: 5px; clear:both;" />Be on the lookout for the September 2013 edition of the Blue and Gold newsletter, which should be arriving in the mail soon. Included in the newsletter are district policies and notifications that we urge you to review, become familiar with and keep for your reference throughout the school year. The issue also provides some [&#8230;] 
]]> 
</description> 
<content:encoded> 
<![CDATA[ 
<img width="150" height="150" src="http://cazhigh.com/caz/gold/wp-content/uploads/2013/08/BluGold_BacktoSchool2013_cover-150x150.jpg" class="attachment-thumbnail wp-post-image" alt="Blue and Gold (Back to School 2013)" style="display: block; margin-bottom: 5px; clear:both;" /><div id="attachment_2898" style="width: 241px" class="wp-caption alignright"><a href="http://cazhigh.com/caz/gold/wp-file-browser-top/blueandgold/Blue%20and%20Gold%20(September%202013)"><img class="size-medium wp-image-2898 " alt="Blue and Gold (Back to School 2013)" src="http://cazhigh.com/caz/gold/wp-content/uploads/2013/08/BluGold_BacktoSchool2013_cover-231x300.jpg" width="231" height="300" /></a><p class="wp-caption-text">Blue and Gold (Back to School 2013)</p></div> <p itemprop="name"><span style="font-size: 13px;">Be on the lookout for the September 2013 edition of the Blue and Gold newsletter, which should be arriving in the mail soon. </span></p> <div itemprop="description articleBody"> <p>Included in the newsletter are district policies and notifications that we urge you to review, become familiar with and keep for your reference throughout the school year.</p> <p>The issue also provides some updates on work undertaken in Cazenovia schools — on everything from curricula to facilities — over the summer break, as well as &#8220;welcome back&#8221; messages from Superintendent of Schools Robert S. Dubik, Cazenovia High School Principal Eric Schnabl and Assistant Principal Susan Vickers, Cazenovia Middle School Principal Jean Regan and Burton Street Elementary Principal Mary-Ann MacIntosh.</p> <p>If you have questions related to the policies or notifications included in the newsletter, please call the district office at (315) 655-1317.</p> <p>The newsletter is also <span style="color: #000080;"><a title="link to Blue and Gold (September 2013)" href="http://cazhigh.com/caz/gold/wp-file-browser-top/blueandgold/Blue%20and%20Gold%20(September%202013)" target="_blank"><span style="color: #000080;"><b>available online</b></span></a></span>.</p> </div> 
]]> 
</content:encoded> 
<wfw:commentRss>http://cazhigh.com/caz/gold/?feed=rss2&p=2896</wfw:commentRss> 
<slash:comments>0</slash:comments> 
</item> 

디버그 : 내가 처음에 "..."각 제목 후에 원인이 무엇인지 파악하지 않은 그러나 enter image description here

+0

여기에 브레이크 포인트를 사용해보십시오 : 그것은 그때 테이블에 출력이 다음과 같이 문자열에서 마지막 세 문자를 문자열 소요되며, 감산 ... 제목으로. 내가 여기서 볼 수있는 것에 대한 나의 우려는 당신이 mutttitle을 초기화한다는 것을 나는 모른다는 것이다. 이상적으로는 iVar가 아닌 속성 일 것입니다. – stevesliva

+0

@stevesliva 디버그를 포함하는 수정 된 게시물 – SuperAdmin

답변

0

또는 그것을 고치는 방법, 나는 그 일을 잘하는 주변의 간단한 일에왔다. `당신이 추가되는지; [문자열 mutttitle appendString]`:

NSString *RSSTitle = nil; 
RSSTitle = [titarry objectAtIndex:indexPath.row]; 
RSSTitle = [RSSTitle substringToIndex:[RSSTitle length] - 3]; 
cell.nameLabel.text= RSSTitle;