Objective c UITableView重新加载数据需要花费很长时间
我通过JSON将一些东西解析到一个表视图中,但我有一个问题,解析大约需要一秒钟(我知道这是由于网络指示器),但是在数据出现在表视图中之前会有很大的延迟 我试图放置[tableView重载数据];已经在几个地方,但没有成功 这是我的密码 我已经将mainThreadQueue和MyClassCoAPI定义为宏Objective c UITableView重新加载数据需要花费很长时间,objective-c,uitableview,reloaddata,Objective C,Uitableview,Reloaddata,我通过JSON将一些东西解析到一个表视图中,但我有一个问题,解析大约需要一秒钟(我知道这是由于网络指示器),但是在数据出现在表视图中之前会有很大的延迟 我试图放置[tableView重载数据];已经在几个地方,但没有成功 这是我的密码 我已经将mainThreadQueue和MyClassCoAPI定义为宏 - (void)viewDidLoad { [super viewDidLoad]; arrayNeuheiten = [[NSArray alloc] init];
- (void)viewDidLoad
{
[super viewDidLoad];
arrayNeuheiten = [[NSArray alloc] init];
arrayArtikelName = [[NSArray alloc] init];
dictionaryNewStuff = [[NSDictionary alloc] init];
[self parseJSONWithURL:myClassicoAPI];
//[self performSelector:@selector(updateTableView) withObject:nil afterDelay:NO];
[neuheietenTable reloadData];
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[neuheietenTable reloadData];
}
-(void) updateTableView {
[neuheietenTable reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfRowsInSection:(NSInteger)section {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//return (self.arrayNeuheiten.count<17)?self.arrayNeuheiten.count : 17;
return MIN(18, arrayNeuheiten.count);
}
-(void) parseJSONWithURL: (NSURL *) jsonURL {
dispatch_async(mainThreadQueue, ^{
NSError *error = nil;
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSString *json =[NSString stringWithContentsOfURL:jsonURL encoding:NSJSONWritingPrettyPrinted error:&error];
if (error == nil) {
NSData *jsonData = [json dataUsingEncoding:NSJSONWritingPrettyPrinted];
dictionaryNewStuff = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
if (error == nil) {
dispatch_async(mainThreadQueue, ^{
arrayArtikelName = [[dictionaryNewStuff valueForKey:@"newstuff"] valueForKey:@"Neuheiten"];
arrayNeuheiten = [[dictionaryNewStuff valueForKey:@"newstuff"] valueForKey:@"Neuheiten"];
[neuheietenTable reloadData];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
});
} else {
nil;
}
} else {
nil;
}
});
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"NeuheitenCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [[arrayArtikelName objectAtIndex:indexPath.row] objectForKey:@"name"];
return cell;
}
-(void)viewDidLoad
{
[超级视图下载];
arrayNeuheiten=[[NSArray alloc]init];
arrayArtikelName=[[NSArray alloc]init];
dictionaryNewStuff=[[NSDictionary alloc]init];
[self-parseJSONWithURL:myClassicAPI];
//[自执行选择器:@selector(updateTableView),对象:nil afterDelay:NO];
[neuheietenTable重载数据];
}
-(无效)视图显示:(BOOL)动画{
[超级视图显示:动画];
[neuheietenTable重载数据];
}
-(void)updateTableView{
[neuheietenTable重载数据];
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(NSInteger)行数区段:(NSInteger)区段{
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
//return(self.arrayNeuheiten.count您正在主线程上同步加载数据,这是错误的,并且阻塞了接口。
尝试使用NSURLConnection加载JSON数据,并在
(void)connectionDidFinishLoading:(NSURLConnection *)aConnection
方法。您好,我有点想用一个公共url修改代码,它的工作正常。请查看有助于解决您的问题的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
arrayNeuheiten = [[NSArray alloc] init];
arrayArtikelName = [[NSArray alloc] init];
dictionaryNewStuff = [[NSDictionary alloc] init];
[neuheietenTable reloadData];
NSURL *url = [NSURL URLWithString:@"http://122.182.14.104:3004/build/product/15.json"];
[self parseJSONWithURL:url];
//[self performSelector:@selector(updateTableView) withObject:nil afterDelay:NO];
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfRowsInSection:(NSInteger)section {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//return (self.arrayNeuheiten.count<17)?self.arrayNeuheiten.count : 17;
return arrayArtikelName.count;
}
-(void) parseJSONWithURL: (NSURL *) jsonURL {
NSError *error = nil;
NSString *json =[NSString stringWithContentsOfURL:jsonURL encoding:NSJSONWritingPrettyPrinted error:&error];
if (error == nil) {
NSData *jsonData = [json dataUsingEncoding:NSJSONWritingPrettyPrinted];
dictionaryNewStuff = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
if (error == nil) {
arrayArtikelName = [dictionaryNewStuff valueForKey:@"attributes"];
// arrayNeuheiten = [[dictionaryNewStuff valueForKey:@"newstuff"] valueForKey:@"Neuheiten"];
[neuheietenTable reloadData];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
} else {
nil;
}
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"NeuheitenCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [[arrayArtikelName objectAtIndex:indexPath.row] objectForKey:@"name"];
return cell;
}
-(void)viewDidLoad
{
[超级视图下载];
arrayNeuheiten=[[NSArray alloc]init];
arrayArtikelName=[[NSArray alloc]init];
dictionaryNewStuff=[[NSDictionary alloc]init];
[neuheietenTable重载数据];
NSURL*url=[NSURL URLWithString:@”http://122.182.14.104:3004/build/product/15.json"];
[自解析JSONWithURL:url];
//[自执行选择器:@selector(updateTableView),对象:nil afterDelay:NO];
}
-(无效)视图显示:(BOOL)动画{
[超级视图显示:动画];
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(NSInteger)行数区段:(NSInteger)区段{
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
//return(self.arrayNeuheiten.count在主线程中像这样调用表重载方法,[self-performSelectorOnMainThread:@selector(reloadData)with object:nil waitUntilDone:YES];如果直接调用reloadData,我会出错。你是指我的void还是字面上的reloadData?[self.tableView-performSelectorOnMainThread:@selector(reloadData)withObject:self-waitUntilDone:YES];我只是移除队列,从json中简单调用数据,然后重新加载表。还有一点,我只有在从json获取数据时才重新加载表。