ios - TableView 的 UIView 无法滚动或点击

标签 ios iphone swift xcode uitableview

我所做的是当单击搜索 Controller 时,它会显示一个带有 tableView 的 View 。 (如 Instagram)。 它显示了 tableView 但无法与之交互。

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

  • Bring subView to the front
  • have set tableView.isUserInteractionEnabled = true -> Have also ran on iPhone
  • Have set tableViewHeight to ScreenHeight

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

这是我的相关代码,如果有帮助的话, 带有搜索栏和 Collection View 的 Controller :

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)
}

Problem to fix: Make tableView scroll and click

先谢谢你!

最佳答案

您的问题是您以错误的方式初始化自定义类,您需要调用 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
}()

关于ios - TableView 的 UIView 无法滚动或点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45913192/

相关文章:

swift - 无法在泛型函数中返回 nil

ios - 如何从源代码构建适用于 iOS 的 libssl.a?

iphone - MFMailComposeViewController 没有解雇

iphone - MPMoviePlayerViewController setFullScreen : shows a white screen

xcode - 在第一个函数完成后调用第二个函数,它是使用 Swift 的 SKAction 重复计数

ios - 在另一个 UIViewController 中处理 UITapGestureRecognizer

objective-c - 如何缩放自定义引脚 MKMapKit

ios - 使用外部添加的依赖项构建 Cocoa Touch 框架

iphone - 如何使用 ASIFormDataRequest 到 NSDictionary 对象的 http post 数组

ios - 如何在 iOS 9 中获取 iphone 设备 token