ios - 为什么点击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
}
解决方案
您需要编写一个方法来执行您需要的操作,并将此操作添加到按钮
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
}
推荐阅读
- android - Lottie 动画在 Android 上出现奇怪错误,适用于 iOS。路径不能在自身上循环
- javascript - 使用 html 数组发送 jquery ajax 发布请求
- javascript - 当我们使用 this.props.history.push("/next Component") 移动到下一个组件时,我们可以传递一个值吗?
- docker - 詹金斯中的权限被拒绝(码头工人中的码头工人)
- python - Python没有正确总结列表
- javascript - 如何导入子对象
- multithreading - OpenCl 中的多线程
- go - 如何格式化包含动态元素数量的字符串?
- sql - 将各种表中的日期存储到 tmp 中的动态查询会产生 NULL
- c++ - 柯南 - 不知道如何制作 vcvarsall.bat