[编辑] 好的,我发现了问题,我只是在调用我的 Web 服务的错误连接请求。所以下载博客会删除文章列表内容。休息时间:p
#我正在升级一个旧应用程序,它是一个简单的RSS 阅读器,带有文章列表和博客列表。这些中的每一个都是一个UITableView
有自己的UITableViewController
.
他们都从我服务器上的 JSON 文件中检索数据。
在JSON
解析结束时,我更新dataSources
然后reloadData
在两个表视图上调用该方法。问题是文章列表充满了博客,而博客列表根本没有填写。
如果我不在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];
}
最后,这是我在 TabBar 中添加这些 TableView 的方法:
- (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;
}