2014-04-09 20 views
0

我正在創建一個rss提要應用程序,我的主視圖控制器將顯示提要。我的應用程序第一次沒有任何錯誤,表現良好。最近,我添加了一個導航菜單,如下所示:http://www.appcoda.com/ios-programming-sidebar-navigation-menu/現在導航菜單不是我的問題。我的rss feed列表視圖完全是空的,它顯示空單元格。這裏是我的主視圖或者Controller.h:我的tableView顯示,但它的空?怎麼修?

#import <UIKit/UIKit.h> 

@interface MainViewController : UIViewController <NSXMLParserDelegate> 

@property (weak, nonatomic) IBOutlet UIBarButtonItem *sidebarButton; 
@property (strong, nonatomic) IBOutlet UITableView *tableView; 

@end 

,這裏是我的主視圖Controller.m或者:

#import "MainViewController.h" 
#import "SWRevealViewController.h" 
#import "DetailViewController.h" 

@interface MainViewController() { 

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

} 

@end 

@implementation MainViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    feeds = [[NSMutableArray alloc] init]; 
    NSURL *url = [NSURL URLWithString:@"http://images.apple.com/main/rss/hotnews/hotnews.rss"]; 
    parser = [[NSXMLParser alloc] initWithContentsOfURL:url]; 
    [parser setDelegate:self]; 
    [parser setShouldResolveExternalEntities:NO]; 
    [parser parse]; 

    // Change button color 
    _sidebarButton.tintColor = [UIColor colorWithWhite:0.1f alpha:0.9f]; 

    // Set the side bar button action. When it's tapped, it'll show up the sidebar. 
    _sidebarButton.target = self.revealViewController; 
    _sidebarButton.action = @selector(revealToggle:); 

    // Set the gesture 
    [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer]; 

} 

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

#pragma mark - Table View 

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return feeds.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
    cell.textLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"title"]; 
    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]; 

    } 

} 

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

     [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]; 
    } 

} 

- (void)parserDidEndDocument:(NSXMLParser *)parser { 

    [self.tableView reloadData]; 

} 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if ([[segue identifier] isEqualToString:@"showDetail"]) { 

     NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
     NSString *string = [feeds[indexPath.row] objectForKey: @"link"]; 
     [[segue destinationViewController] setUrl:string]; 

    } 
} 

@end 

感謝您的幫助!

回答

1

你可能想在以下地點設置斷點:

  • didStartElement:檢查解析開始
  • parserDidEndDocument:檢查是否解析完成並重裝的實現代碼如下
  • 的tableView:numberOfRowsInSection :檢查tableview委託方法是否被打中

你也想檢查tableView的出口以及del egate和數據源,在Interface Builder中正確設置

+0

這會是一個問題嗎?在故事板中,我點擊tableview並轉到連接檢查器,它沒有與數據源或委託的連接。它應該有嗎?另外,myNumberOFRowsInSection沒有被擊中。但是我收到一個錯誤,指出''Receiver()沒有帶標識符'sw_right''的segue這會在應用程序進入numberOfRowsInSection方法之前停止應用程序。 @Z S –

+0

你確定你選擇的是tableview,而不是包含tableview的storyboard嗎?每個tableview(甚至是靜態單元格)都應該在連接檢查器的Outlets部分有一個委託和數據源。 –

+0

是的,我只是雙重檢查,並沒有顯示任何網點。我從來沒有遇到過這個錯誤。你知道這件事嗎? @ Z S –

相關問題