我有一个表格视图在导航栏下面,当用户在搜索栏中键入内容时,它将显示出来。一切似乎都正常。但是当我推出一个视图控制器并在tableview中选中单元格后,从detailViewController返回后,我的tableview就混乱了。
• 问题:在从detailViewController返回之后,tableview最顶部的单元格被导航栏遮挡。但是一开始不会发生这种情况。
• 我已经尝试过:
1. Toggled 'Under Top Bars' in the storyboard
2. Check all my constraint
这是将表格视图添加到子视图的方法。
func updateSearchResults(for searchController: UISearchController) {
// When the user is typing
if let searchText = searchController.searchBar.text, !searchText.isEmpty {
// add the tableview onto the subview
view.addSubview(tableView)
self.searchController.dimsBackgroundDuringPresentation = false
// filter the username array, to match the text in the search bar
filteredUsernames = unfilterdUsernames.filter({ (username) -> Bool in
return username.lowercased().contains(searchText.lowercased())
})
if let usernames = filteredUsernames {
getUsersFromUsernames(usernames: usernames, completion: { (users) in
self.filteredUsers = users
})
}
// else, the user is not typing anything. Remove the tableview from the subview
} else {
self.searchController.dimsBackgroundDuringPresentation = false
tableView.removeFromSuperview()
filteredUsernames = unfilterdUsernames
}
// reload tableview data
tableView.reloadData()
}
这是我进入detailViewController的方式:
在UITableView中,当用户选择某一行时,该方法会被调用。tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
// the profile view that I want to display, after the user select one of the tableViewCell
let profileController = UIStoryboard(name: "Profile", bundle: nil).instantiateViewController(withIdentifier: "profile") as? ProfileCollectionViewController
// user is the value that I want to transfer to the profileController
// which user from the indexPath in tableView
if let user = filteredUsers?[indexPath.row] {
userToPass = user
// set the delegate to self
profileController?.userDataDelegate = self
// push a navigationController to profileController
self.navigationController?.pushViewController(profileController!, animated: true)
进入detailViewController前:
从detailViewController返回后:
第一个索引被导航控制器覆盖了