将数据加载到 UITabBarController 内的 TTTableViewController 中

发布于 2024-10-01 03:29:37 字数 2763 浏览 4 评论 0原文

#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 时,列表永远不会填充任何内容。我将数据输出到控制台,所有数据都被转储到那里,但列表无法填充。谁能解释我做错了什么?

#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

I have this TTTableViewController as the first view of a UITabBarController. It seems to work just fine in loading up the table with the RSS feed data, but when I add it to the UITabBarController, the list never gets populated with anything. I output the data to the console, and it's all being dumped in there, but the list fails to populate. Can anyone explain what i am doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉世弥音 2024-10-08 03:29:37

好的,伙计们,这是 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];

并通过此实例添加子视图控制器。这么说吧,我整个下午都没有脑子。我不知道为什么我认为在选项卡栏实例中需要另一个选项卡栏。至少对于入门者来说这是一个很好的代码示例。

Ok, folks, this is one of those coding DOH! moments, so let me show you my UITabBarController code as well:

#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

SO originally I was creating a tab bar view within a tab bar view i.e. in side the init method, I was doing this:

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

and adding the sub-view controllers via this instance. Let's just say my brain was out for the afternoon. I don't know why I thought to need another tab bar inside a tab bar instance. At least this is a good code example for someone getting started.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文