将数据加载到UITabBarController内的TTTableViewController中

时间:2010-11-06 22:03:07

标签: rss uitabbarcontroller three20

#import "HeadlinesController.h"
#import "DDXML.h"

@implementation HeadlinesController

- (id)init
{
    if(self = [super init]) {
        self.title = @"Headlines";
        self.tabBarItem.image = [UIImage imageNamed:@"166-newspaper.png"];
        self.variableHeightRows = YES;

        self.dataSource = nil;

        [self requestAction];
    }

    return self;
}

- (void)requestAction
{
       TTURLRequest *request = [TTURLRequest requestWithURL:@"http://some.rss" delegate:self];

       request.response = [[[TTURLDataResponse alloc] init] autorelease];
       request.httpMethod = @"GET";
       [request send];
}

#pragma mark -
#pragma mark TTURLRequestDelegate

- (void) requestDidStartLoad:(TTURLRequest *)request
{
    // do nothing for now
}

- (void) requestDidFinishLoad:(TTURLRequest *)request
{
    TTURLDataResponse *response = (TTURLDataResponse *)request.response;
     //NSLog([[NSString alloc] initWithData:response.data encoding:NSUTF8StringEncoding]);
     //NSString *xmlStr = [[NSString alloc] initWithData:response.data encoding:NSUTF8StringEncoding];

     DDXMLDocument *doc = [[DDXMLDocument alloc] initWithData:response.data options:0 error:nil];

     NSArray *resultNodes = nil;
     resultNodes = [doc nodesForXPath:@"//item[position() <= 10]" error:nil];

     TTListDataSource *ds = [[TTListDataSource alloc] autorelease];
     NSMutableArray *dsItems = [[[NSMutableArray alloc] init] autorelease];

     for(DDXMLElement *resultElement in resultNodes) {
         NSString *itemTitle   = [[[resultElement nodesForXPath:@"title" error:nil] objectAtIndex:0] stringValue];
         NSString *itemLink    = [[[resultElement nodesForXPath:@"link" error:nil] objectAtIndex:0] stringValue];
         NSString *itemDesc    = [[[resultElement nodesForXPath:@"description" error:nil] objectAtIndex:0] stringValue];
         NSString *itemPubDate = [[[resultElement nodesForXPath:@"pubDate" error:nil] objectAtIndex:0] stringValue];

         NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
         [df setDateFormat:@"EEE, dd MMMM yyyy HH:mm:ss Z"];
         NSDate *pubDate = [df dateFromString:itemPubDate];

         TTTableMessageItem *tMsgItem = [TTTableMessageItem itemWithTitle:itemTitle caption:@"" text:itemDesc timestamp:pubDate URL:itemLink];
         [dsItems addObject:tMsgItem];
     }

     [ds initWithItems:dsItems];
     self.dataSource = ds;
}

- (void)request:(TTURLRequest *)request didFailLoadWithError:(NSError *)error
{
     NSLog(@"ERROR: %@", error);
}

@end

我将此TTTableViewController作为UITabBarController的第一个视图。它似乎在使用RSS提要数据加载表时工作正常,但是当我将其添加到UITabBarController时,列表永远不会填充任何内容。我将数据输出到控制台,并将其全部转储到那里,但列表无法填充。谁能解释我做错了什么?

1 个答案:

答案 0 :(得分:0)

好的,大家好,这是编码DOH的人之一!片刻,让我告诉你我的UITabBarController代码:

#import "TodayController.h"
#import "HeadlinesController.h"
#import "GalleriesController.h"

@implementation TodayController

- (id)init
{
if(self = [super init]) {
    self.title = @"Today";

    HeadlinesController *headlines = [[HeadlinesController alloc] init];
    GalleriesController *galleries = [[GalleriesController alloc] init];

    [self setViewControllers:[NSArray arrayWithObjects:
                                       headlines,
                                       galleries,
                                       nil]];


    //[self.view addSubview:tabController.view];
}

return self;
}

- (void)dealloc
{
//[tabController release];

[super dealloc];
}

@end

最初我在标签栏视图中创建标签栏视图,即在init方法的一侧,我这样做:

UITabBarController *tabBar = [[[UITabBarController alloc] init] autorelease];

并通过此实例添加子视图控制器。我们只能说我的脑子在下午出去了。我不知道为什么我认为在标签栏实例中需要另一个标签栏。至少这对于某人入门来说是一个很好的代码示例。