【问题标题】:UITableView reload data takes foreverUITableView 重新加载数据需要永远
【发布时间】:2013-09-27 09:20:31
【问题描述】:

我通过 JSON 将一些内容解析到表格视图中,但我遇到的问题是解析需要大约一秒钟(我知道这是由于网络指示器),但是在数据出现在表格视图。

我试图放置 [tableView reloadData];已经在几个地方,但没有成功。

这是我的代码。

我已将 mainThreadQueue 和 myClassicoAPI 定义为宏。

- (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;
}

提前致谢

康斯坦丁

【问题讨论】:

  • 像这样在主线程中调用表重载方法,[self performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
  • 如果我直接调用 reloadData 会出错。你的意思是我的 void 还是字面意义上的 reloadData?
  • [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:self waitUntilDone:YES];

标签: objective-c uitableview reloaddata


【解决方案1】:

您正在主线程上同步加载数据,这很糟糕并阻塞了接口。 尝试使用 NSURLConnection 加载 JSON 数据并在

中重新加载 UITableView
(void)connectionDidFinishLoading:(NSURLConnection *)aConnection

完成处理后的方法。

【讨论】:

    【解决方案2】:

    您好,我用一个公共 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;
    }
    

    【讨论】:

    • 我只是删除队列,只是从 json 中简单地调用数据,然后重新加载表。还有一个,我只有在从 json 获取数据时才重新加载表。
    猜你喜欢
    • 2019-02-08
    • 2023-03-31
    • 2019-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-21
    • 1970-01-01
    相关资源
    最近更新 更多