【问题标题】:table reload.data while enter foreground进入前台时表 reload.data
【发布时间】:2015-10-01 08:08:39
【问题描述】:

早上好 :)

我有一个带有以下示例数据的 tableview 控制器:

测试 1

20.05.2015

测试 2

13.07.2015

检查数据日期的函数。 如果日期 == 今天的日期 => 字体颜色 = 绿色

我的问题: 昨天,数据“Test 2”的字体颜色为黑色=> OK 我让应用程序进入后台并在今天再次打开它。 今天的颜色必须是绿色的,但它也是黑色的。

在我终止应用程序并再次打开后,颜色变为绿色。 我在 ViewillAppear 中有一个 TableLM.reloadData(),但我不工作。 我想,我必须在“applicationWillEnterForeground”函数中重新加载表格

但是 TableLM 在 appdelegate.swift 中是未知的 我该怎么办?

【问题讨论】:

  • 颜色错误..mhm .. 听起来像是细胞重用问题。你在做细胞重复利用吗?你设置 textColor 好吗?

标签: swift uitableview reload


【解决方案1】:

AppDelegate 有处理这种情况的功能。

func applicationWillEnterForeground(application: UIApplication) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

applicationWillEnterForeground 中调用通知并重新加载tableView

【讨论】:

    【解决方案2】:

    在 Swift 3.1 中

    NotificationCenter.default.addObserver(self, selector: #selector(YourViewController.appBecomeActive), name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil )
    

    【讨论】:

      【解决方案3】:

      在您的viewDidLoad 方法中添加此代码:

      NSNotificationCenter.defaultCenter().addObserver(self, selector: "appBecomeActive", name: UIApplicationWillEnterForegroundNotification, object: nil )
      

      之后添加这个方法,当应用程序进入前台时重新加载你的tableview:

      func appBecomeActive() {
          //reload your Tableview here
          TableLM.reloadData()
      }
      

      【讨论】:

      • 非常感谢 - 我可以工作 :) 但为什么 TableLM.reloadData() 只能在没有 NSNotificationCenter 的 ViewWillAppear 中工作?
      猜你喜欢
      • 2021-12-27
      • 2014-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多