2012-10-22 30 views
-1

我在iOS應用程序的表格視圖中遇到RSS提要的問題。我最初使用表視圖作爲根視圖來測試項目中的RSS源。我試圖在不同的項目中獲得相同的功能,但顯示RSS源中文章列表的表格視圖是空白的。新項目中表格視圖的代碼是相同的。唯一的不同是我有一個不同的根視圖控制器,它有一堆按鈕。一個按鈕應該轉到該表視圖,但它的行卻是空的。我想這可能是一個問題,如何設置根視圖控制器,因爲我知道當代碼自己運行時填充表視圖。以下是它設置根視圖控制器的AppDelegate文件。UITableView有空行

// 
// KFBAppDelegate.h 
// KFBNewsroom 
// 
// Created by KFB on 10/15/12. 
// Copyright (c) 2012 com.kfb. All rights reserved. 
// 

#import <UIKit/UIKit.h> 

@class KFBViewController; 

@interface KFBAppDelegate : UIResponder <UIApplicationDelegate> 

@property (strong, nonatomic) UIWindow *window; 

@property (strong, nonatomic) KFBViewController *viewController; 

@end 


// 
// KFBAppDelegate.m 
// KFBNewsroom 
// 
// Created by KFB on 10/15/12. 
// Copyright (c) 2012 com.kfb. All rights reserved. 
// 

#import "KFBAppDelegate.h" 
#import "KFBViewController.h" 
#import "ListViewController.h" 
#import "WebViewController.h" 
#import "ActionAlertsViewController.h" 
#import "MarketUpdatesViewController.h" 

@implementation KFBAppDelegate 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions 
{ 
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
// Override point for customization after application launch. 
self.viewController = [[KFBViewController alloc] initWithNibName:@"KFBViewController" bundle:nil]; 
ListViewController *lvc = [[ListViewController alloc]initWithStyle:UITableViewStylePlain]; 
WebViewController *wvc = [[WebViewController alloc]init]; 
[lvc setWebViewController:wvc]; 
ActionAlertsViewController *avc = [[ActionAlertsViewController alloc]initWithStyle:UITableViewStylePlain]; 
[avc setWebViewController:wvc]; 
MarketUpdatesViewController *mvc = [[MarketUpdatesViewController alloc]initWithStyle:UITableViewStylePlain]; 
[mvc setWebViewController:wvc]; 
self.window.rootViewController = self.viewController; 
self.window.backgroundColor = [UIColor whiteColor]; 
[self.window makeKeyAndVisible]; 
return YES; 
} 






    // 
    // ListViewController.h 
    // Nerdfeed 
    // 
    // Created by KFB on 10/16/12. 
    // Copyright (c) 2012 com.kfb. All rights reserved. 
    // 

    #import <Foundation/Foundation.h> 

    // @interface ListViewController : NSObject 

    // a forward declaration; we'll import the header in the .m 
    @class RSSChannel; 
    @class WebViewController; 

    @interface ListViewController : UITableViewController 
    <NSXMLParserDelegate> 
    { 
     NSURLConnection *connection; 
     NSMutableData *xmlData; 
     RSSChannel *channel; 
    } 
    @property (nonatomic, strong)WebViewController *webViewController; 

    - (void)fetchEntries; 


    @end 





// 
// ListViewController.m 
// Nerdfeed 
// 
// Created by KFB on 10/16/12. 
// Copyright (c) 2012 com.kfb. All rights reserved. 
// 

#import "ListViewController.h" 
#import "RSSChannel.h" 
#import "RSSItem.h" 
#import "WebViewController.h" 

@implementation ListViewController 
@synthesize webViewController; 

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary  *)attributeDict 
{ 
    NSLog(@"%@ found a %@ element", self, elementName); 
    if ([elementName isEqual:@"channel"]) 
    { 
     // If the parser saw a channel, create new instance, store in our ivar 
     channel = [[RSSChannel alloc]init]; 

     // Give the channel object a pointer back to ourselves for later 
     [channel setParentParserDelegate:self]; 

     // Set the parser's delegate to the channel object 
     [parser setDelegate:channel]; 
    } 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // return 0; 

    return [[channel items]count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // return nil; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"]; 
    } 
    RSSItem *item = [[channel items]objectAtIndex:[indexPath row]]; 
    [[cell textLabel]setText:[item title]]; 

    return cell; 
} 

- (void)fetchEntries 
{ 
    // Create a new data container for the stuff that comes back from the service 
    xmlData = [[NSMutableData alloc]init]; 

    // Construct a URL that will ask the service for what you want - 
    // note we can concatenate literal strings together on multiple lines in this way - this results in a single NSString instance 
    NSURL *url = [NSURL URLWithString:@"http://kyfbnewsroom.com/category/public- affairs/feed"]; 

    // Put that URL into an NSURLRequest 
    NSURLRequest *req = [NSURLRequest requestWithURL:url]; 

    // Create a connection that will exchange this request for data from the URL 
    connection = [[NSURLConnection alloc]initWithRequest:req delegate:self  startImmediately:YES]; 
} 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 

    if (self) 
    { 
     [self fetchEntries]; 
    } 

    return self; 
} 

// This method will be called several times as the data arrives 
- (void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data 
{ 
    // Add the incoming chunk of data to the container we are keeping 
    // The data always comes in the correct order 
    [xmlData appendData:data]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)conn 
{ 
    /* We are just checking to make sure we are getting the XML 
    NSString *xmlCheck = [[NSString alloc]initWithData:xmlData  encoding:NSUTF8StringEncoding]; 
    NSLog(@"xmlCheck = %@", xmlCheck);*/ 

    // Create the parser object with the data received from the web service 
    NSXMLParser *parser = [[NSXMLParser alloc]initWithData:xmlData]; 

    // Give it a delegate - ignore the warning here for now 
    [parser setDelegate:self]; 

    //Tell it to start parsing - the document will be parsed and the delegate of NSXMLParser will get all of its delegate messages sent to it before this line finishes execution - it is blocking 
    [parser parse]; 

    // Get rid of the XML data as we no longer need it 
    xmlData = nil; 

    // Reload the table.. for now, the table will be empty 
    [[self tableView]reloadData]; 

    NSLog(@"%@\n %@\n %@\n", channel, [channel title], [channel infoString]); 
} 

- (void)connection:(NSURLConnection *)conn didFailWithError:(NSError *)error 
{ 
    // Release the connection object, we're done with it 
    connection = nil; 

    // Release the xmlData object, we're done with it 
    xmlData = nil; 

    // Grab the description of the error object passed to us 
    NSString *errorString = [NSString stringWithFormat:@"Fetch failed: %@", [error localizedDescription]]; 

    // Create and show an alert view with this error displayed 
    UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"Error" message:errorString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [av show]; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Push the web view controller onto the navigation stack - this implicitly creates the web view controller's view the first time through 
    [[self navigationController]pushViewController:webViewController animated:YES]; 

    // Grab the selected item 
    RSSItem *entry = [[channel items]objectAtIndex:[indexPath row]]; 

    // Construct a URL with the link string of the item 
    NSURL *url = [NSURL URLWithString:[entry link]]; 

    // Construct a request object with that URL 
    NSURLRequest *req = [NSURLRequest requestWithURL:url]; 

    // Load the request into the web view 
    [[webViewController webView]loadRequest:req]; 

    // Set the title of the web view controller's navigation item 
    [[webViewController navigationItem]setTitle:[entry title]]; 
} 



@end 

回答

0

確保您設置了UITableView數據源,以便您可以實際顯示行。您可能也需要設置委託。

編輯: 你還沒有設置UITableView委託或數據源。你需要這樣做,以便UITableView知道從哪裏獲取數據。我認爲你把那個最好的地方是在你的ListViewController的viewDidLoad方法

-(void)viewDidLoad { 
    [super viewDidLoad]; 
    [tableView setDelegate: self]; 
    [tableView setDataSource: self]; 
} 

您還需要添加的UITableViewDelegate和UITableViewDataSource協議ListViewController。

編輯2:

確保您要添加的UITableViewDataSource和的UITableViewDelegate協議ListViewController

@interface ListViewController : UITableViewController 
<NSXMLParserDelegate, UITableViewDelegate, UITableViewDataSource> 

此外,確保上述viewDidLoad方法是在ListViewController類。所有UITableViewControllers都有一個tableView property,它包含實際的UITableView。

+0

下面是表視圖,視圖控制器: – RagingGoat

+0

我當我補充說明時會出現錯誤。 「沒有已知的選擇器'setDataSource'的類方法'」和「沒有選擇器的類方法setDelegate'」 – RagingGoat

+0

然後無論你調用那些選擇器都不是UITableView。確保你調用的是UITableView,而不是UITableViewController。 – hukir

0

你ListViewController類必須從UITableViewController中

編輯繼承:對不起,我沒看出來,只是添加代理&數據源

@interface ListViewController : UITableViewController <NSXMLParserDelegate, UITableViewDelegate, UITableViewDataSource>

你加:

-(void)viewDidLoad { 
[super viewDidLoad]; 
[tableView setDelegate: self]; 
[tableView setDataSource: self]; 

}

關於ListViewController的實現(像@synthesize webViewController; 之後)?

+0

我做了所有這一切,並得到沒有已知類選擇器'setDataSource'的方法和沒有已知的類選擇器'setDelegate'方法 – RagingGoat

+0

實際上,當它被放在像上面,我得到未知的接收器'tableView',你的意思是UITableView ? – RagingGoat

+0

我得到的代碼工作,但表視圖中的行仍然是空的。 – RagingGoat

0

嘗試從ListViewController.h

刪除此行
@property (nonatomic, retain) UITableView *tableView; 

那麼這段代碼替換ListViewController.m您- (void) viewDidLoad

- (void) viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self.tableView setDelegate: self]; 
    [self.tableView setDataSource:self]; 
} 
+0

表視圖仍然爲空 – RagingGoat

+0

因此,我在tableView:cellForRowAtIndexPath處放置了一個斷點:這是我收到的另一個建議。點擊按鈕後,該應用程序繼續運行以進入該視圖,以便部分代碼甚至不會被執行。 – RagingGoat

+0

有關於此的任何想法? – RagingGoat