如何解决Swift:您如何将数据从表格视图单元传递到按钮?
我试图在编辑时传递tableview单元格数据确实以按钮结尾。我一直在使用标签,但一直出现错误nil。我认为这与我拥有的行数(超过500行)有关。有人知道最好的方法吗?
下面的代码是我使用的(标签)。只有当我有多个tableviewcell行并且必须滚动到页面时,它才会崩溃(返回nil)。
func numberOfSections(in tableView: UITableView) -> Int { return 1 }
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int { return inventoryArray.count }
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let code : String = (inventoryArray[indexPath.row] as AnyObject).value(forKey: "code") as! String
let name : String = (inventoryArray[indexPath.row] as AnyObject).value(forKey: "name") as! String
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell",for: indexPath) as! InventoryCell
cell.unitField.tag = indexPath.row
cell.unitField.addTarget(self,action: #selector(Inventory.submit(_:)),for: UIControl.Event.editingDidEnd)
return cell as InventoryCell
}
...
@IBAction func submit(_ sender: UIButton) {
let tag = sender.tag
let cell = tableView.cellForRow(at: IndexPath.init(row: tag,section: 0)) as! InventoryCell
var unitCount = Int(cell.unitField.text!)!
cell.count.text = String (unitCount)
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。