我在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
答案 0 :(得分: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。
答案 1 :(得分:0)
您的ListViewController类必须从UITableViewController继承
编辑:抱歉,我没有看到它,只需添加代表&amp; dataSource
@interface ListViewController : UITableViewController
<NSXMLParserDelegate, UITableViewDelegate, UITableViewDataSource>
你添加了:
-(void)viewDidLoad {
[super viewDidLoad];
[tableView setDelegate: self];
[tableView setDataSource: self];
}
关于ListViewController的实现(类似于@synthesize webViewController;
之后)?
答案 2 :(得分:0)
尝试从ListViewController.h中删除此行
@property (nonatomic, retain) UITableView *tableView;
然后用以下代码替换ListViewController.m中的- (void) viewDidLoad
:
- (void) viewDidLoad
{
[super viewDidLoad];
[self.tableView setDelegate: self];
[self.tableView setDataSource:self];
}