-1

iOS アプリのテーブル ビューで RSS フィードに問題があります。私はもともと、ルート ビューとしてテーブル ビューを使用して、そのプロジェクトで RSS フィードをテストしました。別のプロジェクトで同じ機能を取得しようとしていますが、RSS フィードからの記事のリストを表示するテーブル ビューが空白です。新しいプロジェクトのテーブル ビューのコードは同じです。唯一の違いは、多数のボタンを持つルート ビュー コントローラーが異なることです。1 つのボタンがそのテーブル ビューに移動するはずですが、行は空です。そのコードが単独で実行されたときにテーブルビューが作成されることがわかっているので、これはルートビューコントローラーの設定方法に問題がある可能性があると考えています。ルート ビュー コントローラーを設定する 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

実際に行を表示できるように、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クラスにあることを確認してください。すべてのUITableViewControllerには、実際のUITableViewを保持するtableViewプロパティがあります。

于 2012-10-22T18:16:13.453 に答える
0

ListViewController クラスは UITableViewController から継承する必要があります

編集:申し訳ありませんが、表示されませんでした。デリゲートとデータソースを追加するだけです

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

追加しましたか:

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

}

ListViewController の実装について (after のように@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 に答える