2017-08-28 111 views
1

我所做的是当点击搜索控制器时,它显示一个包含tableView的View。 (如Instagram)。 它显示tableView,但它不能与它交互。TableView的UIView无法滚动或点击

我在做一些研究,因为人们在这之前就遇到过这个。 这里是我试过的事情:

  • 把子视图前
  • 已设置tableView.isUserInteractionEnabled = true - >刚才还跑在iPhone上
  • 先后成立tableViewHeight到ScreenHeight

但tableView仍然不想滚动/点击!

下面是相关的代码,我有,如果有帮助, 控制器,搜索栏和收集意见

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 
} 

下面是对的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) 
} 

问题需要解决:请滚动的tableView并点击

谢谢先进!

+0

为什么在设置其他属性后重新初始化tableView?在setupTv func – Joshua

回答

1

你的问题是,你是在错误的方式初始化您的自定义类,需要调用SearchUsersTv(frame:而不是SearchUsersTv()用于初始化,因为所有的tableView设置上setupTv()发生被称为在SearchUsersTv(frame:初始化仅

取代你tableView由此内联创建

let tableView: UIView = { 
    let screenHeight = UIScreen.main.bounds.height 
    let screenWidth = UIScreen.main.bounds.width 
    let tv = SearchUsersTv(frame: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight)) 
    tv.isUserInteractionEnabled = true 
    tv.bringSubview(toFront: tv) 
    tv.clipsToBounds = false 
    tv.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellId") 
    return tv 
}() 
+0

AH了解它与上面的代码和现在理解为什么。谢谢! –