UITableview在添加观察者后没有更新值?

时间:2015-12-10 12:55:21

标签: ios objective-c

我有两个 UIViewController

UIViewController-1 UIViewController-2

UIViewController-1 UIButton 名称作为过滤器

当我点击过滤器按钮时,它会将 UIViewController-2 UIView添加到 UIViewController-1

通过此代码

- (IBAction)filterAction:(id)sender {
    if (isDown==NO) {
        isDown=YES;
        UIViewController* rvc=[self.storyboard instantiateViewControllerWithIdentifier:@"SearchCriteriaViewController"];
        self.overlayView=rvc.view;
        CGRect rect=self.overlayView.frame;
        rect.origin.y=self.overlayView.frame.origin.y+110;
        rect.size.height=self.overlayView.frame.size.height-160;
        self.overlayView.frame=rect;
        [self addChildViewController:rvc];
        [self.view addSubview:self.overlayView];
        [rvc didMoveToParentViewController:self];
    } else {
        isDown=NO;
        [self.overlayView removeFromSuperview];
    }
}

它的工作好。

UIViewController-2 有一些以 UIButton 名称作为过滤器的过程。

当我点击过滤器按钮时,它会从服务器获取一些值并将其存储到 NSDictonary

我想将值传递给 UIViewController-1

所以我正在使用 NSNotificationCenter

首先删除了 UIViewController-2 ,然后我发布了通知。

[self willMoveToParentViewController:nil];
[self.view removeFromSuperview];
[self removeFromParentViewController];
[[NSNotificationCenter defaultCenter] postNotificationName:@"postArrayObject" object:response];

UIViewController-1 中,我收到了Observer。

-(void)viewWillAppear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(objFirstViewController:) name:nil object:nil];
}

-(void)objFirstViewController:(NSNotification *)notification {
    if ([[notification name]isEqualToString:@"postArrayObject"]) {
        self.arrayFromSearch=[NSMutableArray arrayWithArray:(NSMutableArray *)[notification object]];
        NSLog(@"%@",self.arrayFromSearch);
        [eventList removeAllObjects];
        NSMutableDictionary *dictionary;
        for (NSDictionary *dic in self.arrayFromSearch) {
            dictionary=[NSMutableDictionary dictionary];
            [dictionary setObject:[dic valueForKey:@"eventDate"] forKey:@"EVENTDATE"];
            [dictionary setObject:[dic valueForKey:@"eventId"] forKey:@"EVENTID"];
            if ([[dic valueForKey:@"country"] isKindOfClass:[NSNull class]]) {
                [dictionary setObject:@"" forKey:@"COUNTRY"];
            }else{
                [dictionary setObject:[dic valueForKey:@"country"] forKey:@"COUNTRY"];
            }if ([[dic valueForKey:@"eventImage"] isKindOfClass:[NSNull class]]){
                [dictionary setObject:@"" forKey:@"EVENTIMAGE"];
            }else{
                [dictionary setObject:[dic valueForKey:@"eventImage"] forKey:@"EVENTIMAGE"];
            }if([[dic valueForKey:@"state"] isKindOfClass:[NSNull class]]){
                [dictionary setObject:@"" forKey:@"STATE"];
            }else{
                [dictionary setObject:[dic valueForKey:@"state"] forKey:@"STATE"];
            }
            [dictionary setObject:[dic valueForKey:@"eventName"] forKey:@"EVENTNAME"];
            [dictionary setObject:[dic valueForKey:@"eventTime"] forKey:@"EVENTTIME"];
            [dictionary setObject:[dic valueForKey:@"isFeature"] forKey:@"FEATURE"];
            [dictionary setObject:[NSString stringWithFormat:@"%@",[dic valueForKey:@"latitue"]] forKey:@"LAT"];
            [dictionary setObject:[NSString stringWithFormat:@"%@",[dic valueForKey:@"langitude"]] forKey:@"LANG"];
            [eventList addObject:dictionary];
        }

        // [self.listTableView reloadData];
        // [self.listTableView performSelectorOnMainThread:@selector(reloadTable) withObject:nil waitUntilDone:YES];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.listTableView reloadData];
        });

        previousSeletedIndex=[[NSUserDefaults standardUserDefaults]integerForKey:@"last"];
        if (previousSeletedIndex>5) {
            [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"last"];
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:previousSeletedIndex inSection:0];

            [self.listTableView scrollToRowAtIndexPath:indexPath
                                      atScrollPosition:UITableViewScrollPositionBottom
                                              animated:YES];
        }else{
        }
    }
    else{            
    }
}

调用Everthing,我从 Notificaton对象获取值并将其存储到本地 NSMutableArray

当我重新加载UITableView而不更新值

但是会调用数据源和代理。

2 个答案:

答案 0 :(得分:1)

终于找到了解决方案。 我不知道确切的原因,但我的理解  它阻止了主线程

dispatch_async(dispatch_get_main_queue(), ^{
            [self.listTableView reloadData];
        });
        [self.listTableView setNeedsLayout];

添加此代码,其woking

答案 1 :(得分:0)

当您添加观察者时,您需要传递name

[[NSNotificationCenter defaultCenter] addObserver:self 
                                      selector:@selector(objFirstViewController:) 
                                      name:@"postArrayObject" 
                                      object:nil];