【问题标题】:UIView of TableView can't scroll or tapTableView 的 UIView 无法滚动或点击
【发布时间】:2018-02-05 09:08:07
【问题描述】:

我所做的是当单击搜索控制器时,它会显示一个带有 tableView 的视图。 (如 Instagram)。 它显示了tableView,但不能与之交互。

我正在做一些研究,因为人们在此之前遇到过这个问题。 以下是我尝试过的方法

  • 将 subView 置于最前面
  • 已设置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 滚动并单击

提前谢谢你!

【问题讨论】:

  • 设置其他属性后为什么要重新初始化tableView?在 setupTv 函数中

标签: ios iphone swift xcode uitableview


【解决方案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
}()

【讨论】:

  • AH 用上面的代码搞定了,现在明白为什么了。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-11
  • 2018-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多