Ios dispatch\u async UITableView重载数据
我有一个问题,当启动viewDidLoad方法时,数据会在UITableView中正确加载和显示,但当我必须通过clickPopular方法重新加载数据时,TableView不会更新 有什么办法可以做到这一点吗 视图加载方法Ios dispatch\u async UITableView重载数据,ios,objective-c,uitableview,nsmutablearray,dispatch-async,Ios,Objective C,Uitableview,Nsmutablearray,Dispatch Async,我有一个问题,当启动viewDidLoad方法时,数据会在UITableView中正确加载和显示,但当我必须通过clickPopular方法重新加载数据时,TableView不会更新 有什么办法可以做到这一点吗 视图加载方法 -(void)viewDidLoad { Name = [[NSMutableArray alloc] init]; slug = [[NSMutableArray alloc] init]; Immagine = [[NSMutableArray alloc] init]
-(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];
});
});
}
-(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"]];
}
}
}
流行方法
-(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];
});
});
}
-(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"]];
}
}
}
加载JSON方法
-(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];
});
});
}
-(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"]];
}
}
}
重新加载时,您永远不会清除阵列 这意味着在分派\u async之前重新加载时,旧条目仍会保留
[Name removeAllObjects];
[slug removeAllObjects];
[Immagine removeAllObjects];
[visit eremoveAllObjects];
[categorie removeAllObjects];
实际上。。作为-(iAction)clickPopular:(id)sender的第一行执行此操作{在
viewdiload
中不要调用startcaricanto
。最好不要在viewdiload
中初始化NSMutableArray
startcaricanto是一个启动activityImageView的无效方法。我应该把init NSMutableArray放在哪里?@simonly在视图中初始化NSMutableArray
有什么问题DidLoad?将data init方法放在viewController init中会更好。因为在iOS6之前,如果内存警告出现,当视图再次出现时,viewDidLoad将被调用,并且您可能会得到一个空白界面。[myArray removeAllObjects];添加新的,然后重新加载数据也重新考虑您的命名…变量以小写字母开头,我会使用复数作为数组名称。我尝试过,但它返回一个错误:***由于未捕获的异常“NSRangeException”终止应用程序,原因:'***-[\uu NSArrayM objectAtIndex:]:索引0超出空数组“***
很抱歉,无法了解您正在执行的操作。但这就是问题所在。您没有向我们展示的某些代码可能正在尝试读取旧数据。某些情况下,这些数据访问也应在主线程中完成(在调度异步(调度获取主队列(),^{
)。否则,当后台队列(DISPATCH_QUEUE_PRIORITY_DEFAULT)正在清除并向这些数组添加新对象。如果UITableView试图使用其持有的旧数据,它将崩溃。