【问题标题】:TableView cannot detect when clicking on specific item点击特定项目时 TableView 无法检测到
【发布时间】:2021-04-15 06:23:51
【问题描述】:

嘿,我有一个正常工作的 UITable,它可以从我的数组中加载项目,但现在我需要在单击 UITable 的特定项目时进行调用

我的视图控制器代码下面有我自己的类,我可以像这样创建它:

class TableDataSource: NSObject, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let identifier = "\(UITableViewCell.self)"
        let item = items[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: identifier) ?? UITableViewCell(style: .subtitle, reuseIdentifier: identifier)
        cell.textLabel?.text = item
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("lol")
    }
    
    var items: [String] = []
    
    func attach(to view: UITableView) {
        // Setup itself as table data source (Implementation in separated extension)
        view.dataSource = self
        // Register element for dequeuing (All dequeuing element must register in table before)
        view.register(UITableViewCell.self, forCellReuseIdentifier: "\(UITableViewCell.self)")
    }
}

如您所见,我有一个 didSelectRowAt,它应该只打印“lol”作为测试,但这不起作用

为了初始化这个 UITable,我确实这样称呼它

    @IBOutlet weak var TableItemsView: UITableView!
     private let dataSource = TableDataSource()
    var nutList = ["empty"]

我从 firebase 调用我的数组并将其应用于 nutList:

 if let document = document, document.exists {
                self.nutList = document.get("nutList") as! [String]
                self.dataSource.attach(to: self.TableItemsView)
                self.dataSource.items = self.nutList
        } else {
                print("No food...")
        }

一切正常,它显示所有项目和东西,但是当我点击一个单独的项目时,它不会打印我想要的东西。有什么想法吗?

【问题讨论】:

    标签: ios swift uitableview tableview swift5


    【解决方案1】:

    didSelectRowAt 属于 UITableViewDelegate。如果您想在需要采用协议的同一类中实现此方法

    class TableDataSource: NSObject, UITableViewDataSource, UITableViewDelegate { ...
    

    并在attach(to中设置表格视图的delegate

    func attach(to view: UITableView) {
        // Setup itself as table data source (Implementation in separated extension)
        view.dataSource = self
        view.delegate = self
        // Register element for dequeuing (All dequeuing element must register in table before)
        view.register(UITableViewCell.self, forCellReuseIdentifier: "\(UITableViewCell.self)")
    }
    

    【讨论】:

      【解决方案2】:

      您需要在viewDidLoad 中设置您的委托和数据源。

      应该是这样的:

      override func viewDidLoad() {
              super.viewDidLoad()
              tableView.delegate = self
              tableView.dataSource = self
          }
      

      【讨论】:

      • 在将我的类重构为我的视图控制器代码后,我可以确认这种方法也有效
      猜你喜欢
      • 1970-01-01
      • 2012-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-01
      • 1970-01-01
      相关资源
      最近更新 更多