Loading data into TTTableViewController inside a 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 TTURLRequestD开发者_如何学Goelegate
- (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?
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.
精彩评论