简体   繁体   English

如何在单击 tabBar 项 Swift 上刷新 tableView?

[英]How to refresh a tableView on click tabBar item Swift?

I have a tableView into a viewController on tab Bar.我有一个 tableView 到选项卡栏上的 viewController 中。 When I click into tabBar item the tableView isn't refreshing.当我点击 tabBar 项目时,tableView 没有刷新。 But I have the viewWillAppear function:但我有 viewWillAppear 功能:

 override func viewWillAppear(animated: Bool) {
    dispatch_async(dispatch_get_main_queue(), { () -> Void in
        self.tableView.reloadData()
    })
}

I'm trying call this function with tab bar delegate, but not works:我正在尝试使用标签栏委托调用此函数,但不起作用:

 func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
     dispatch_async(dispatch_get_main_queue(), { () -> Void in
        self.tableView.reloadData()
    })
 }

Why not reload?为什么不重新加载?

Thanks!谢谢!

You do not need to involve the tab bar controller in this.您不需要在此涉及选项卡栏控制器。 To do so would be overkill.这样做会矫枉过正。 You'll create confusing code that somebody will want to rip out later.您将创建令人困惑的代码,稍后有人会想要撕掉这些代码。 Updating the table view in viewWillAppear(_:) is almost always the best approach.viewWillAppear(_:)更新表视图几乎总是最好的方法。

If everything is set up properly, your view controller's viewWillAppear(_:) method will get called each time the user selects that tab and your view becomes visible.如果一切都设置正确,则每次用户选择该选项卡并且您的视图变为可见时,您的视图控制器的viewWillAppear(_:)方法都会被调用。 So if it's not getting called, you have a problem somewhere in the design of your tab bar and its view controllers.因此,如果它没有被调用,那么您的标签栏及其视图控制器的设计就存在问题。

Create a new project and select the Tabbed Application template.创建一个新项目并选择选项卡式应用程序模板。 Open the SecondViewController file and add a viewWillAppear(_:) method.打开 SecondViewController 文件并添加一个viewWillAppear(_:)方法。 Add a breakpoint there.在那里添加一个断点。 You'll see it's called every time you switch to the second tab.每次切换到第二个选项卡时,您都会看到它被调用。

Some other thoughts...还有一些想法……

You'll notice in the viewWillAppear(_:) documentation that it says you must always call super.您会在viewWillAppear(_:)文档中注意到它说您必须始终调用 super。 You're not.你不。 You should.你应该。 But this is unrelated to your problem.但这与您的问题无关。

Also, there's no need to switch to the main queue.此外,无需切换到主队列。 viewWillAppear(_:) will always be called on the main queue. viewWillAppear(_:)将始终在主队列上调用。

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    tableView.reloadData()
}

Answer for swift 5.0快速 5.0 的答案

Reload UITableView in main thread and then check.在主线程中重新加载UITableView然后检查。 I hope this will work for you.我希望这对你有用。

override func viewWillAppear(_ animated: Bool) {
    DispatchQueue.main.async {  
        self.tableView.reloadData()
    }
}

You can find the answer here: open viewcontroller from tab like in instagram camera page on IOS您可以在这里找到答案: 像在 IOS 上的 Instagram 相机页面中一样,从选项卡中打开 viewcontroller

Basically, you need to create a controller which inherits from UITabBarController and set it in the Storyboard to the custom class of your tabBarView.基本上,您需要创建一个继承自 UITabBarController 的控制器,并将其在 Storyboard 中设置为您的 tabBarView 的自定义类。

Then set Tags to each Tab via the Storyboard.然后通过情节提要为每个选项卡设置标签。

Afterwards you can use the delegate method to call an action if a specific tab was clicked.之后,如果单击特定选项卡,您可以使用委托方法调用操作。

The delegate method should look like this:委托方法应如下所示:

override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
    switch item.tag{
    case 1: //code here
         break
    default: break
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM