Erik Batista Erik Batista - 2 months ago 14
iOS Question

UIView of TableView can't scroll or tap

What I did was when the search controller is clicked, it shows a View with a tableView in it. (like Instagram) . It shows the tableView but it cannot interact with it.

I was doing some research since people had come across this before here. Here are the things I've tried:



  • Bring subView to the front

  • have set
    tableView.isUserInteractionEnabled = true
    -> Have also ran on iPhone

  • Have set tableViewHeight to ScreenHeight




But the tableView still doesn't want to scroll/click!

Here's the relevant code that I have if it helps,
Controller with search bar and collection views:

class UserSearchController: UICollectionViewController, UICollectionViewDelegateFlowLayout,UISearchBarDelegate, UISearchDisplayDelegate {

let cellId = "cellId"

let searchBar: UISearchBar = {
let sb = UISearchBar()
sb.placeholder = "Search"
return sb
}()
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
searchBar.setShowsCancelButton(true, animated: true)
tableView.isHidden = false
}

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.setShowsCancelButton(true, animated: true)

}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
searchBar.setShowsCancelButton(false, animated: true)
searchBar.text = ""
tableView.isHidden = true
}

let tableView: UIView = {
let tv = SearchUsersTv()
tv.isUserInteractionEnabled = true
tv.bringSubview(toFront: tv)
tv.clipsToBounds = false
return tv
}()

override func viewDidLoad() {
super.viewDidLoad()

collectionView?.register(UserProfileVideoCell.self, forCellWithReuseIdentifier: cellId)

view.addSubview(tableView)
tableView.isHidden = true
}


Here's the code for the relevant code for the tableView (SearchUsersTv):

class SearchUsersTv: UIView, UITableViewDelegate, UITableViewDataSource {

let cellId = "cellId"
var tableView = UITableView()

override init(frame: CGRect){
super.init(frame: frame)
setupTv()
}

func setupTv() {
let screenHeight = UIScreen.main.bounds.height
let screenWidth = UIScreen.main.bounds.width
tableView.delegate = self
tableView.dataSource = self
tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
tableView.isUserInteractionEnabled = true
tableView = UITableView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight))
self.addSubview(tableView)
bringSubview(toFront: tableView)
}



Problem to fix: Make tableView scroll and click


Thank you in advanced!

Answer Source

Your problems is that you are initialising your custom class in wrongly way, you need call SearchUsersTv(frame: instead of SearchUsersTv() for initialisation because all your tableView setup happen on setupTv() which is called in SearchUsersTv(frame: initialisation only

replace your tableView inline creation by this

let tableView: UIView = {
    let tv = SearchUsersTv(frame:CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
    tv.isUserInteractionEnabled = true
    tv.bringSubview(toFront: tv)
    tv.clipsToBounds = false
    return tv
}()