[编辑] 好的我发现了问题,我只是调用了我的Web服务的错误连接请求。因此,下载博客会删除文章列表内容。休息时间:p
#我正在升级旧应用程序,这是一个简单的 RSS阅读器,其中包含文章列表和博客列表。其中每个都是UITableView
,其中包含UITableViewController
个JSON
。
他们都从我服务器上的JSON文件中检索数据。
在dataSources
解析结束时,我更新reloadData
,然后在两个tableviews上调用reloadData
方法。问题是文章列表中充满了博客,博客列表根本没有填充。
如果我没有在博客tableview
上致电tableview.tag
,那么第一条内容就会正确填写并且运作良好。正如在另一篇文章中看到的那样,我尝试使用IB
标识符,但它不起作用。
注意:我没有使用BlogTableViewController
。
以下是@implementation BlogsTVC
@synthesize dataSourceForBlogs;
@synthesize blogRec;
- (id)initWithStyle:(UITableViewStyle)style
{
if ((self = [super initWithStyle:style])) {
self.title = @"Blogs";
self.tableView.tag = 2;
self.tableView.delegate = self;
dataSourceForBlogs = [[NSMutableArray alloc] init];
// Register to notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callback_blogs:) name:@"blogs" object:nil ];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(connection_error:) name:@"connection_error" object:nil];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self refreshBlogs];
}
-(void)callback_blogs:(NSNotification *)notification
{
if([self.dataSourceForBlogs count] > 0)
{
NSInteger i, count = [self.dataSourceForBlogs count];
for(i=0; i < count; i++)
{
[[self.dataSourceForBlogs objectAtIndex:i] release];
}
[self.dataSourceForBlogs removeAllObjects];
}
//remplissage
NSArray *receivedDatas = [[notification userInfo] objectForKey:@"data"];
NSUInteger i, count = [receivedDatas count];
for(i=0; i<count; i++)
{
self.blogRec = [[BlogRecord alloc] init];
self.blogRec.blog_id_auteur = [[receivedDatas objectAtIndex:i] objectForKey:@"id"];
self.blogRec.blog_auteur = [[receivedDatas objectAtIndex:i]objectForKey:@"auteur"];
self.blogRec.blog_url = [[receivedDatas objectAtIndex:i]objectForKey:@"url"];
self.blogRec.blog_date = [[receivedDatas objectAtIndex:i] objectForKey:@"date"];
self.blogRec.blog_nb_pub = [[receivedDatas objectAtIndex:i] objectForKey:@"nb_publication"];
self.blogRec.blog_url_image = [[receivedDatas objectAtIndex:i] objectForKey:@"theme"];
[self.dataSourceForBlogs addObject:self.blogRec];
self.blogRec = nil;
}
[self.tableView reloadData];
}
的代码:
@implementation ActualiteTVC
@synthesize dataSource;
@synthesize artRec;
#pragma mark -
#pragma mark Initialization
- (id)initWithStyle:(UITableViewStyle)style {
// Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
if ((self = [super initWithStyle:style])) {
// Titles
self.title = @"Actualités";
self.tableView.tag = 1;
// Register to notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callback_general:) name:@"general" object:nil ];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(connection_error:) name:@"connection_error" object:nil];
self.dataSource = [[NSMutableArray alloc] init];
}
return self;
}
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
[self refreshNews];
}
-(void)callback_general: (NSNotification *) notification
{
if ([self.dataSource count] > 0) {
NSUInteger i, count = [self.dataSource count];
for (i = 0; i < count; i++) {
[[self.dataSource objectAtIndex:i] release];
}
[self.dataSource removeAllObjects];
}
NSArray *receivedDatas = [[notification userInfo] objectForKey:@"data"];
NSUInteger i, count = [receivedDatas count];
for (i = 0; i < count; i++) {
self.artRec = [[ArticleRecord alloc] init];
self.artRec.art_id = [[receivedDatas objectAtIndex:i] objectForKey:@"id"];
self.artRec.art_auteur = [[receivedDatas objectAtIndex:i] objectForKey:@"auteur"];
self.artRec.art_categorie = [[receivedDatas objectAtIndex:i] objectForKey:@"categorie"];
self.artRec.art_date = [[receivedDatas objectAtIndex:i] objectForKey:@"date"];
//self.artRec.art_texte = [[receivedDatas objectAtIndex:i] objectForKey:@"texte"];
self.artRec.art_titre = [[receivedDatas objectAtIndex:i] objectForKey:@"titre"];
self.artRec.art_url_auteur = [[receivedDatas objectAtIndex:i] objectForKey:@"url_auteur"];
self.artRec.art_url = [[receivedDatas objectAtIndex:i] objectForKey:@"url"];
self.artRec.art_url_img = [[receivedDatas objectAtIndex:i] objectForKey:@"image"];
[self.dataSource addObject:self.artRec];
self.artRec = nil;
}
[self.tableView reloadData];
}
以下是文章的代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 0
//articles
ActualiteTVC *actualiteTVC = [[ActualiteTVC alloc] init];
UINavigationController *actualiteNC = [[UINavigationController alloc] initWithRootViewController:actualiteTVC];
[actualiteTVC release];
actualiteNC.tabBarItem.title = @"Actualités";
actualiteNC.tabBarItem.image = [UIImage imageNamed:@"08-chat.png"];
[actualiteNC.navigationBar setBarStyle:UIBarStyleBlack];
//1
//blogs
BlogsTVC *blogsTVC = [[BlogsTVC alloc] init];
UINavigationController *blogsNC = [[UINavigationController alloc] initWithRootViewController:blogsTVC];
[blogsTVC release];
blogsNC.tabBarItem.title = @"Blogs";
blogsNC.tabBarItem.image = [UIImage imageNamed:@"balance.png"];
[blogsNC.navigationBar setBarStyle:UIBarStyleBlack];
// myTabBarController
myTabBarController = [[UITabBarController alloc] init];
[myTabBarController setViewControllers:[NSArray arrayWithObjects:actualiteNC, blogsNC, nil]];
[actualiteNC release];
[blogsNC release];
/*
//fullscreen
CGRect frame = [[UIScreen mainScreen] applicationFrame];
frame.origin.x = 0;
frame.origin.y = 25;
//[baseViewCtrl.view setFrame:frame];
[myTabBarController.view setFrame:frame];
*/
/*
// window
//[window addSubview:myTabBarController.view];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleRemoveLoading:) name:@"removeLoading" object:nil];
loading = [[LoadingVC alloc] init];
[window addSubview:loading.view];
*/
[window addSubview:baseViewCtrl.view];
[window makeKeyAndVisible];
return YES;
}
最后,我将在TabBar中添加这些TableView:
{{1}}