【问题标题】:iOS TableView not reloading if current view is tableViewController如果当前视图是 tableViewController,iOS TableView 不会重新加载
【发布时间】:2012-06-26 07:24:21
【问题描述】:
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
    // Handle the notificaton when the app is running

    NSLog(@"Recieved Notification %@",notif);

    NSLog(@"local notifications count = %d", [[[UIApplication sharedApplication] scheduledLocalNotifications] count]);
}

这是来自应用委托的方法,当通知到达时我需要重新加载表格视图。

我该如何实现reloadData,因为如果我写“[TableViewController.tableView reloadData];”Xcode 不会接受?

【问题讨论】:

    标签: iphone ios xcode tableview reloaddata


    【解决方案1】:
    //Just do one thing, as you got the notification , post on more notification as follows in the method ....
    
    - (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"RELOAD_DATA"object:nil];
    }
    
    //the add observer in viewDidLoad of that view controller where your table is added..
    
     [[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(receiveTableNotification:) 
        name:@"RELOAD_DATA"
        object:nil];
    
    //and make a method in the same class 
    
    - (void)receiveTableNotification:(NSNotification *)pNotification{
        [your_table_view reloadData];
    }
    
    //now remove obser in dealloc in your view controller class where you add observer .. 
    
    - (void) dealloc
    {
    
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        [super dealloc];
     }
    

    【讨论】:

    • 宾果游戏!!!!!!!伙计,你刚刚解决了一个我正在尝试 1 周的问题。超级感谢。真的。我无法解释我现在有多开心。
    • @Abhishek 我们可以在不通知的情况下这样做吗?我正在迅速做这件事
    【解决方案2】:

    [self.viewController.tableView reloadData]代替TableViewController.tableView

    如果你当前的视图Controller不是tableView,考虑使用NSNotificationCenter发布重载通知

    【讨论】:

    • 你能给我发一个这样的例子吗?我是 iOS 新手
    • 你试过用这个 [self.viewController.tableView reloadData] 吗?
    • 是的,当我写 [self.TableViewController.tableView reloadData] Xcode 给我“在‘AppNameAppDelegate’类型的对象上找不到属性‘TableViewController’
    • 发布你的代码 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions,这样我就可以看到你在做什么了
    • 我在这个 AppDelegate 方法中一无所获。只是“返回是”我应该在上面写些什么吗?
    【解决方案3】:

    使用NSNotificationCenter postNotification: 机制发布一个reloadTable 通知,您可以在类中通过observer 捕获具有tableViewController 的reloadData 来为您的tableview 触发reloadData

    【讨论】:

      【解决方案4】:

      请在回调函数中的 app deligate 中调用它

      - (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
      
            UINavigationController *navControl = [[[self tableViewController] viewControllers] objectAtIndex:lastObject]; //
               id Obj = [[navControl viewControllers] lastObject];
                      if([Obj isKindOfClass:[Required class]])
                      {
                         [Obj reloadDataOfTable];// made an function to desired class.
                      }
      }
      

      【讨论】:

      • 我把它放在这里吗?
      • 你需要做一些改变......一点点......根据你的类名和tableviewcontroller名字
      • 问题是 Xcode 不会接受我的 tableView 名称。当我写 [self TableViewController] 时,我收到错误说不存在这样的属性
      • 请看appdeligate类.h文件,看tableviewcontroller的名字...你会得到它....你也可以用.(点运算符)调用它
      猜你喜欢
      • 1970-01-01
      • 2013-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多