我非常坚持这一点,并希望有人可以向我展示一些有关此事的信息。 我需要帮助来弄清楚如何从幻灯片菜单中刷新主视图上的表格列表。
主要观点
基本上,我有一个主视图,其中包含一系列事件。我使用此函数从数据库中检索数据:
[self loadEvents:eventCat sortEvent:nil searchDate:dateSelected];
在该函数内部,它具有获取数据的脚本,确定是否有内容然后重新加载表:
- (void)loadEvents:(NSString*)searchType sortEvent:(NSString*)sortType searchDate:(NSString *)dateSelect{
NSString *url = [NSString stringWithFormat: @"http://www.url.com/eventList.php];
NSURL *eventURL = [NSURL URLWithString: url];
NSLog(@"%@", eventURL);
NSData *jsonData = [NSData dataWithContentsOfURL: eventURL];
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error: &error];
self.eventList = [NSMutableArray array];
NSArray *eventArray = [dataDictionary objectForKey:@"fitpass"];
//NSString *eventFound = [dataDictionary objectForKey:@"notfound"];
//if nothing is found
if ([dataDictionary objectForKey:@"notfound"]){
tableView.hidden = YES;
noEvent.hidden = NO;
NSLog(@"123");
}else{
noEvent.hidden = YES;
tableView.hidden = NO;
NSLog(@"RELOADDATA");
for(NSDictionary *eventDictionary in eventArray){
Event *event = [Event blogPostWithTitle:[eventDictionary objectForKey:@"name"]];
event.eventTime = [eventDictionary objectForKey:@"event_time"];
[self.eventList addObject:event];
[tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}
}
我尝试使用NSNotificationCenter方法刷新数据。所以我在viewDidLoad方法中有这个:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshTableWithNotification:) name:@"RefreshTable" object:nil];
然后在ViewDidLoad方法之后执行此功能:
- (void)refreshTableWithNotification:(NSNotification *)notification
{
[self.tableView reloadData];
}
在幻灯片菜单视图中
在侧边菜单中,我有一个表格列表来填充不同的类别,当使用时点击一个类别,它应该刷新主视图中的事件列表:
//on direct press, trigger segue
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//get current value and set the eventCategory ID in userDefault
NSDictionary *catPicked = [self.catList objectAtIndex:indexPath.row];
NSString *eventCat =[catPicked objectForKey:@"ID"];
NSLog(@"%@Picked:", eventCat);
[userDefaults setObject:eventCat forKey:kEventCat];
[userDefaults synchronize];
NSLog(@"%@entered", [userDefaults objectForKey:kEventCat]);
//redirect back to eventList;
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier: @"eventListViewController"];
//refresh data
EventLists *destinationController = [[EventLists alloc] init];
[destinationController loadEvents:eventCat sortEvent:nil searchDate:nil];
//send notofication over
[[NSNotificationCenter defaultCenter] postNotificationName:@"RefreshTable" object:nil userInfo:nil];
[destinationController.view setNeedsDisplay];
[[SlideNavigationController sharedInstance] popToRootAndSwitchToViewController:vc
withSlideOutAnimation:self.slideOutAnimationEnabled
andCompletion:nil];
return;
}
我被困的是我可以看到数据(eventList)已更新,但视图没有刷新......
非常感谢任何帮助!!!
答案 0 :(得分:0)
罗里!!!
非常感谢你的帮助!!我终于用你的暗示想出来了。 所以基本上我认为是因为我的日志传递了他的数据。但它只是运行该功能。没有数据通过。
解决方案实际上相当简单。由于数据实际上没有被传递,因此刷新通知函数中的表将不起作用。需要重新加载数据。换句话说,我只需要将主视图中的通知功能更改为:
- (void)refreshTableWithNotification:(NSNotification *)notification
{
[self loadEvents:[userDefaults objectForKey:kEventCat] sortEvent:nil searchDate:dateSelected];
}
然后我的侧面菜单视图现在变得更加简单,因为我需要的只是通知调用和视图开关:
//redirect back to eventList;
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier: @"eventListViewController"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"RefreshTable" object:nil userInfo:nil];
是的!我需要将主视图中的通知移动到ViewDidAppear:
- (void)viewDidAppear:(BOOL)animated{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshTableWithNotification:) name:@"RefreshTable" object:nil];
}
非常感谢你的帮助!!