我有一个应用程序,其UITabelView
从服务器端获取其内容。在我的应用程序中,我每隔30秒从服务器读取内容....因为我使用{{1} }。
当我加载包含NSTimer
的视图时,此NSTimer
会被初始化,当我离开此视图时,它会失效。
我的问题是:
如果在服务器端,UITableView
的内容使用新项目更新,而iphone应用程序中收到的UITableView
作为对服务器请求的响应包含该项目....屏幕上显示的JSON
仍未更新。
我是这样做的:
//加载此视图时启动计时器,并调用方法UITableView
repeatServerRequest
//方法repeatServerRequest在后台启动一个新线程,它向服务器发送请求以下载内容
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
if(playlistTimer == nil)
playlistTimer = [NSTimer scheduledTimerWithTimeInterval:30.0 target: self selector: @selector(repeatServerRequest) userInfo: nil repeats: YES];
}
因此,当我更新服务器端的内容时,我在服务器端获得的JSON作为响应被更新,但UITAbelView不是, - (void) repeatServerRequest{
[NSThread detachNewThreadSelector:@selector(backgroundThinking) toTarget:self withObject:nil];
}
- (void) backgroundThinking{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSURL *url = [NSURL URLWithString:@"a link to server"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
[pool release];
}
///when the response from server comes in these methods are called:
- (void)requestFinished:(ASIHTTPRequest *)request
{
[self performSelectorOnMainThread:@selector(didFindAnswer:) withObject:request waitUntilDone:YES];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(@"the value of error %@", error);
}
- (void) didFindAnswer:(ASIHTTPRequest *) request{
NSLog(@"update tabel");
SBJSON *parser = [[SBJSON alloc] init];
NSString *responseString = [request responseString];
NSArray *statuses = [parser objectWithString:responseString error:nil];
streams = [statuses valueForKey:@"_playLists"];
[parser release];
playList = [[NSMutableArray alloc] init];
idList = [[NSMutableArray alloc] init];
int ndx;
for (ndx = 0; ndx<streams.count; ndx++) {
NSDictionary *stream = (NSDictionary *)[streams objectAtIndex:ndx];
[playList addObject:[stream valueForKey:@"name"]];
[idList addObject:[stream valueForKey:@"id_playlist"]];
}
NSLog(@"playList %@", playList);
oneview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 480, 320)];
tableViewPlaylist =[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
tableViewPlaylist.bounces=NO;
tableViewPlaylist.backgroundColor=[UIColor clearColor];
[tableViewPlaylist setDelegate:self];
[tableViewPlaylist setDataSource:self];
}
。任何想法为什么?
答案 0 :(得分:0)
两个主要问题:
你应该做的是不要创建一个新的tableView。在主线程上更新数据模型后调用[tableView reloadData]
。你的桌子会更新,一切都很好。假设您已正确配置所有内容,并且听起来就像您在启动应用程序时看到预期数据一样。