我有一个tableView,当用户选择一个单元格时,会加载一个详细视图。我希望允许用户通过单击上一个按钮或下一个按钮来浏览表示tableView中项目的详细视图。如何将其实现到我的详细视图控制器中?
使detailViewController成为一个水平的CollectionView,启用了分页,并且与父viewController具有相同的数据源。您只需传入segue中的当前indexPath并在viewDidLoad中滚动到该indexPath。
从你的datasource
传递相同的tableViewController
到detailViewController
。同时从tableViewCell
将所选detailViewController
的索引传递给func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath).
现在,当用户点击detailViewController
上的下一个或上一个按钮时,更新您选择的索引。根据点击的下一个或上一个按钮递增或递减selectedIndex
。从基于object
的数组中获取当前的selectedIndex
并更新您的UI。
@IBAction func nextButtonClicked(_ sender: Any) {
selectedIndex += 1
}
@IBAction func previousButtonClicked(_ sender: Any) {
selectedIndex -= 1
}
func updateDetailView() {
object = dataSource[selectedIndex]
}