将数据加载到 UITabBarController 内的 TTTableViewController 中
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,伙计们,这是 DOH 的编码之一!现在,让我也向您展示我的 UITabBarController 代码:
所以最初我是在选项卡栏视图中创建一个选项卡栏视图,即在 init 方法中,我正在这样做:
并通过此实例添加子视图控制器。这么说吧,我整个下午都没有脑子。我不知道为什么我认为在选项卡栏实例中需要另一个选项卡栏。至少对于入门者来说这是一个很好的代码示例。
Ok, folks, this is one of those coding DOH! moments, so let me show you my UITabBarController code as well:
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:
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.