-2

我有一个问题,当启动 viewDidLoad 方法时,数据在 UITableView 中正确加载并显示,但是当我必须通过 clickPopular 方法重新加载数据时,TableView 没有更新。

关于如何做到这一点的任何想法?

viewDidLoad 方法

-(void)viewDidLoad
{
Name = [[NSMutableArray alloc] init];
slug = [[NSMutableArray alloc] init];
Immagine = [[NSMutableArray alloc] init];
visite = [[NSMutableArray alloc] init];
categorie = [[NSMutableArray alloc] init];

[[NSUserDefaults standardUserDefaults] setObject:@"recent" forKey:@"settings_home_filter"];
    [[NSUserDefaults standardUserDefaults] synchronize];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self LoadJson];

        dispatch_async(dispatch_get_main_queue(), ^{
            [tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];

            [tableView reloadData];
            [self StopCaricamento];
        });
    });
}

流行方法

-(IBAction)clickPopular:(id)sender{
    [tableView reloadData];

    [[NSUserDefaults standardUserDefaults] setObject:@"popular" forKey:@"settings_home_filter"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [self StartCaricamento];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self LoadJson];

        dispatch_async(dispatch_get_main_queue(), ^{
            [tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];

            [tableView reloadData];
            [self StopCaricamento];
        });
    });
}

LoadJson 方法

-(void)LoadJson
{

    NSString *filtro = [[NSUserDefaults standardUserDefaults] objectForKey:@"settings_home_filter"];

    NSString *stringachiamata = [NSString stringWithFormat:@"https://www.mywebsite.com/videos/latest?count=100&ln=en&result_type=%@", filtro];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:stringachiamata]];
    [request setHTTPMethod:@"GET"];
    [request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];

    NSError *err;
    NSURLResponse *response;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

    if(err != nil)
    {
        NSLog(@"Error Parsing JSON: %@", err);
    }
    else
    {
        NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];
        array = [jsonArray objectForKey:@"videos"];

        NSLog(@"%d", [array count]);

        for (int i = 0; i < [array count]; i++)
        {            
            [Name addObject:[[array objectAtIndex:i] objectForKey:@"name"]];
            [slug addObject:[[array objectAtIndex:i] objectForKey:@"slug_video"]];
            [Immagine addObject:[[array objectAtIndex:i] objectForKey:@"thumbnail_video_original"]];
            [visite addObject:[[array objectAtIndex:i] objectForKey:@"views_video"]];
            [categorie addObject:[[array objectAtIndex:i] objectForKey:@"category_name_video"]];
        }

    }
}

StartCaricamento 和 Stop Caricamento 方法

-(void)StartCaricamento{
    activityImageView.hidden = NO;
    [activityImageView startAnimating];
}

-(void)StopCaricamento{
    [activityImageView stopAnimating];
    activityImageView.hidden = YES;
}
4

1 回答 1

3

重新加载时您永远不会清除数组...

意味着旧条目在您 dispatch_async 之前重新加载时仍然存在

[Name removeAllObjects];
[slug removeAllObjects];
[Immagine removeAllObjects];
[visit eremoveAllObjects];
[categorie removeAllObjects];

实际上.. 将其作为 -(IBAction)clickPopular:(id)sender{ 的第一行

于 2014-03-08T13:30:10.210 回答