2017-08-28 109 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瞭解它與上面的代碼和現在理解爲什麼。謝謝! –