首页 > 解决方案 > 为什么点击tableViewCell中的按钮时只返回一个uid?

问题描述

我向您提出的问题是,我在表格视图单元格中有一个按钮,当点击一个人名时,它会显示当前用户 uid,但事实是,当我点击另一个人名时,它应该返回该用户 uid 而不是当前的,能否请您帮我修复此错误提前谢谢您,如果您不完全理解我的代码,请随时询问更多代码

func numberOfSections(in tableView: UITableView) -> Int {

    return array.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}     




 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


     //This is a button inside the tableView cell
    cell.buttonName.setTitle(array[indexPath.section].uid ,for: .normal)
    //this is putting the name inside another ViewController label and display its name 
    self.secondViewController.name.text = array[indexPath.section].uid
 cell.buttonName.addTarget(self, action: #selector(addName), for: .touchUpInside) 
 }
   let person = secondViewController()


@objc func addName() {

    person.name.text = array[?].uid
    profiler.hidesBottomBarWhenPushed = true


}

标签: iosswiftfirebasefirebase-realtime-database

解决方案


您需要编写一个方法来执行您需要的操作,并将此操作添加到按钮

func updatePersonName(_ button:UIButton) {
    self.secondViewController.name.text = button.titleLabel.text
}     

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


     //This is a button inside the tableView cell
    cell.buttonName.setTitle(array[indexPath.section].uid ,for: .normal)
    //We remove all actions added to this button (reuse)
    cell.buttonName.removeTarget(self, action: nil, for: .allEvents)
    //Add new target
    cell.buttonName.addTarget(self, action:#selector(updatePersonName), for: .touchUpInside)

    return cell
 }

推荐阅读