当用户使用 UITableView 的后退按钮滚动到最后选择的行时 [英] When user uses back button to UITableView to scroll to last selected row

查看:27
本文介绍了当用户使用 UITableView 的后退按钮滚动到最后选择的行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大约 200 行的 tableView.当用户选择一行查看详细信息然后点击后退按钮时,tableView 总是返回到表格的顶部.

I have a tableView with about 200 rows. When a user selects a row to view the details and later hits the back button, the tableView always returns to the top of the table.

我想设置 tableView 以强制 tableView 自动滚动到用户上次选择的行.

I would like to set up the tableView to force the tableView to auto scroll to the row in which the user last selected.

如何做到这一点?

推荐答案

我建议将选定的 NSIndexPath 存储在 didSelectRowAtIndexPath 方法中(当用户按下一个单元格时).然后在 viewWillAppearviewDidAppear 方法(在你的表视图所在的视图控制器中)调用

I would suggest storing selected NSIndexPath in the didSelectRowAtIndexPath method (when user presses on a cell). Then in viewWillAppear or viewDidAppear method (in view controller where your table view is) just call

tableView.scrollToRow(at: selectedIndexPath, at: .top, animated: true)

然后它会将您的表格视图滚动到先前选择的单元格.

It will then scroll your table view to previously selected cell.

这篇关于当用户使用 UITableView 的后退按钮滚动到最后选择的行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆