-1

我在 iOS 应用程序的表格视图中遇到 RSS 提要问题。我最初在它的项目中以表格视图作为根视图测试了 RSS 提要。我试图在不同的项目中获得相同的功能,但显示来自 RSS 提要的文章列表的表格视图是空白的。新项目中表格视图的代码是相同的。唯一不同的是我有一个不同的根视图控制器,它有一堆按钮。一个按钮应该转到该表视图,它确实可以,但行是空的。我认为这可能是如何设置根视图控制器的问题,因为我知道当代码自行运行时会填充表格视图。这是设置根视图控制器的 AppDelegate 文件。

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

3 回答 3

0

您的 ListViewController 类必须继承自 UITableViewController

编辑:对不起,我没有看到它,只需添加委托和数据源

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

您是否添加:

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

}

关于 ListViewController 的实现(如之后@synthesize webViewController; )?

于 2012-10-22T20:20:22.837 回答
0

尝试从 ListViewController.h 中删除这一行

 @property (nonatomic, retain) UITableView *tableView;

然后用- (void) viewDidLoad以下代码替换您在 ListViewController.m 中的内容:

- (void) viewDidLoad
{
   [super viewDidLoad];
   [self.tableView setDelegate: self];
   [self.tableView setDataSource:self];
}
于 2012-10-24T15:26:01.817 回答
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 属性来保存实际的 UITableView。

于 2012-10-22T18:16:13.453 回答