返回时重新加载 UITableView?

发布于 2024-09-24 06:05:17 字数 415 浏览 1 评论 0原文

我有一个包含 UITableView 的顶级 UIViewController 。顶层 UIViewController 实例化一个 NavigationController,并将另一个 UIViewController 推送到 NavigationController 上。 IE 我们进入了一个新的第二视图。第二个视图的左上角有通常的“后退”按钮,可让您导航回顶层视图。

当从第二个视图导航回顶级视图时,是否可以通过调用 cellForRowAtIndexPath 使用第二个视图中生成的数据在顶级视图中重绘 UITableView code> 在顶层,如果是这样,如何做到这一点?

I have a top level UIViewController that contains a UITableView. The top level UIViewController instantiates a NavigationController, and pushes another UIViewController onto the NavigationController. I.E. we push into a new, second view. This second view has the usual "Back" button in the upper left hand corner to allow you to navigate back to the top level view.

Is it possible, when navigating back to the top level view from the second view, to redraw the UITableView in the top level view, using data generated in the second view, by calling cellForRowAtIndexPath in the top level and if so, how does one do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

故人爱我别走 2024-10-01 06:05:17

您需要做的就是这样(在具有 UITableView 的 UIViewController 中)。此时您不必担心细胞水平上会发生什么。

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tableView reloadData]; // to reload selected cell
}

只需将上面的代码添加到您的控制器中,当您从第二个视图返回时,您就会看到正确的事情发生了。对 reloadData 的调用告诉表视图它需要刷新模型中的单元格,其他一切都会顺利进行。

All you need to do is this (in your UIViewController that has a UITableView). You don't have to worry about what happens at cell-level at this point.

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tableView reloadData]; // to reload selected cell
}

Just add the code above to your controller, and you'll see that the right thing happens when you come back from your second view. The call to reloadData tells the table view that it needs to refresh its cells from your model, and everything else follows nicely.

忘年祭陌 2024-10-01 06:05:17

如果您只想重新加载选定的单元格,请在 UITableViewController 的自定义子类中重写 viewWillAppear:,如下所示:

- (void)viewWillAppear:(BOOL)animated
{
    NSIndexPath *selectedRowIndexPath = [self.tableView indexPathForSelectedRow];
    [super viewWillAppear:animated]; // clears selection
    if (selectedRowIndexPath) {
        [self.tableView reloadRowsAtIndexPaths:@[selectedRowIndexPath] withRowAnimation:UITableViewRowAnimationNone];
    }
}

注意:假设您已离开 clearsSelectionOnViewWillAppear< /code> 设置为 YES(默认),您必须在调用 super 之前获取所选行的索引路径,这会清除选择。

此外,@ZoranSimic 的解决方案仅调用 [self.tablView reloadData] 是可以接受的,因为它的代码更少,但仍然高效。

最后,也许保持表格视图的单元格与它们所代表的模型对象同步的最佳方法是像 NSFetchedResultsController 那样进行操作并使用键值观察(KVO)和/或 NSNotification code> 当模型对象发生更改时通知您的表视图控制器,以便它可以重新加载相应的单元格。表视图控制器可以在 viewDidLoad 中开始观察其模型对象的变化,并在 dealloc 中结束观察(以及任何手动卸载 self.view< /代码>)。

If you only want to reload the cell that was selected, override viewWillAppear: in your custom subclass of UITableViewController like so:

- (void)viewWillAppear:(BOOL)animated
{
    NSIndexPath *selectedRowIndexPath = [self.tableView indexPathForSelectedRow];
    [super viewWillAppear:animated]; // clears selection
    if (selectedRowIndexPath) {
        [self.tableView reloadRowsAtIndexPaths:@[selectedRowIndexPath] withRowAnimation:UITableViewRowAnimationNone];
    }
}

NOTE: Assuming you've left clearsSelectionOnViewWillAppear set to YES (the default), you must get the index path of the selected row before calling super, which clears the selection.

Also, the solution of @ZoranSimic to just call [self.tablView reloadData] is acceptable as it's less code and still efficient.

Finally, perhaps the best way to keep your table view's cells in sync with the model objects they represent is to do like NSFetchedResultsController and use key-value observing (KVO) and/or NSNotification to inform your table view controller when model objects have changed so that it can reload the corresponding cells. The table view controller could begin observing changes to its model objects in viewDidLoad and end observing in dealloc (and anywhere you manually unload self.view).

寂寞花火° 2024-10-01 06:05:17

除了 Zoran Simic 的回答之外,还有 Swift 代码:

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

In addition to the answer of Zoran Simic here the Swift code:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    tableView.reloadData()
}
戒ㄋ 2024-10-01 06:05:17

您可以实现 UINavigationControllerDelegate 的方法并创建弹出视图控制器时刷新的逻辑。

另一种方法是在表视图控制器上实现 viewWillAppear 上的一些逻辑,以根据弹出的视图控制器的数据进行刷新。

如果您需要将数据从第二个视图控制器发送到第一个视图控制器,请记住您的第二个视图控制器“不”知道前一个视图控制器的存在。它应该是透明的。您应该实现一些协议以使第二个视图控制器将数据发送到第一个视图控制器。这将是解决此问题的第三个选项,因为您的第一个视图控制器将是委托,第二个视图控制器将向委托发送信息,您可以根据以下内容准备要重新加载的表视图(第一个视图控制器)收到数据。

You could implement the methods of UINavigationControllerDelegate and create the logic for the refreshing when popping view controllers.

Another way to do this would be implementing on your table view controller some logic on viewWillAppear, to refresh based on the data of the popped view controller.

If you need to send data from the second view controller to the first view controller, remember that your second view controller "doesn't" know the existence of the previous view controller. It should be transparent for it. You should implement some protocol to make the second view controller send data to the first view controller. It would be a third option to this problem, since your first view controller would be the delegate and the second view controller would be sending info to the delegate, you could be preparing your table view (first view controller) to reload, based on the received data.

落花浅忆 2024-10-01 06:05:17

每次视图出现时(包括第一次)都会调用 viewWillAppear
这是我的解决方案,仅当您导航回来时才重新加载视图

我在 ViewController.h 文件 中创建一个变量,以检查这是我第一次进入此页面ViewController 或当我导航回来

@property (nonatomic) BOOL isViewExist;

之后在 viewWillAppear

-(void)viewWillAppear:(BOOL)animated{
    if(!self.isViewExist){
        self.isViewExist = YES; // check it is the first time you go to this ViewController -> don't reload anything
    }else{
        [self.tableView reloadData]; // to reload the tableview data
        // or your can refresh anything in your ViewController as you want
    }
}

希望这有帮助

viewWillAppear gets called every time the view appears include the first time.
Here is my solution for reload the view only when you navigate back

I create one variable in .h file of my ViewController for checking that it is the first time I go to this ViewController or when I navigate back

@property (nonatomic) BOOL isViewExist;

After that in viewWillAppear

-(void)viewWillAppear:(BOOL)animated{
    if(!self.isViewExist){
        self.isViewExist = YES; // check it is the first time you go to this ViewController -> don't reload anything
    }else{
        [self.tableView reloadData]; // to reload the tableview data
        // or your can refresh anything in your ViewController as you want
    }
}

Hope this help

滥情空心 2024-10-01 06:05:17

也许现在可以提供帮助。

对于 Swift 2+

在 vi​​ewDidAppear 方法中:

如果您想重新加载所有表格:

tableView.reloadData()

如果您只想重新加载选定的单元格。

let index = tableView.indexPathForSelectedRow
if (index != nil){
  self.tableView.reloadRowsAtIndexPaths([index], withRowAnimation: UITableViewRowAnimation.Automatic)
}

Maybe can help now.

For Swift 2+

In your viewDidAppear method:

If you want to reload all table:

tableView.reloadData()

If you want to reload only the cell selected.

let index = tableView.indexPathForSelectedRow
if (index != nil){
  self.tableView.reloadRowsAtIndexPaths([index], withRowAnimation: UITableViewRowAnimation.Automatic)
}
入怼 2024-10-01 06:05:17

更新为 Swift 3

let index = tableView.indexPathForSelectedRow
    if (index != nil){
        self.tableView.reloadRows(at: [index!], with: UITableViewRowAnimation.automatic)
    }

Updated for Swift 3

let index = tableView.indexPathForSelectedRow
    if (index != nil){
        self.tableView.reloadRows(at: [index!], with: UITableViewRowAnimation.automatic)
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文