Swift:如何将数据从tableview单元格传递到按钮?
我正在尝试将编辑结束时的tableview单元格数据传递到按钮。我一直在使用标签,但我不断得到一个错误,无发现。我想这就是我拥有的行数(超过500行)。有人知道最好的方法吗 下面的代码是我使用的(标记)。只有当我有多个tableviewcell行并且必须滚动页面时,它才会崩溃(返回nil)。Swift:如何将数据从tableview单元格传递到按钮?,swift,uitableview,Swift,Uitableview,我正在尝试将编辑结束时的tableview单元格数据传递到按钮。我一直在使用标签,但我不断得到一个错误,无发现。我想这就是我拥有的行数(超过500行)。有人知道最好的方法吗 下面的代码是我使用的(标记)。只有当我有多个tableviewcell行并且必须滚动页面时,它才会崩溃(返回nil)。 func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ table
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
}
您可以尝试使用以下方法来管理按钮操作,而不是使用标记:首先尝试避免强制强制转换,并使用quards或if语句来处理它。也许某个地方的文字是零。你喜欢哪一行?现在也很清楚了。你有两个桌面视图吗?因为在一个示例中,您在另一个InventoryCell01中使用了InventoryCell?让我们了解一下它是如何在一个tableview中工作的。Alexander Nikolaychuck-Crash位于let cell=tableView.cellForRow(位于:IndexPath.init(row:tag,section:0))as!库存单元
@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)
}