[英]How do I set the text of a custom cell's textfield to a variable inside a custom object array in Swift?
[英]Appending Custom Cell Label and TextField text to Array of Dictionary
我有一个带有UILabel和UITextField的自定义tableView单元格。 我想将带有标签的textField中的数据附加到字典数组[[label:textfield])。 我可以使用textFieldDidEndEditing获取textField数据,但是我不确定如何获取同一文本字段的单元格标签。 我认为我需要访问该单元格的indexPath。 我尝试将通知发送到DidSelectRow,但这似乎太复杂了。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: expenseCell, for: indexPath) as! LineItemTableViewCell
let sectionsArray = expenses[sections[indexPath.section]]
let expenseItem = sectionsArray?[indexPath.row]
cell.budgetLineItemView.label.text = expenseItem!
cell.budgetLineItemView.lineItemTextField.delegate = self
return cell
}
//MARK: UITextField Delegate
func textFieldDidBeginEditing(_ textField: UITextField) {
textField.layer.borderColor = UIColor.blue.cgColor
textField.keyboardType = .decimalPad
textField.becomeFirstResponder()
}
func textFieldDidEndEditing(_ textField: UITextField) {
//get textfield text after editing here
}
这是我的字典:
var expenses:[String:[[String:Double]]] = ["Housing":[["Rent/Mortgage":0.0],
["Gas":0.0],["Water/Power":0.0],["Cable/Internet":0.0],["Garbage":0.0],["Property
Tax":0.0],["Homeowners/Renters Insurance":0.0]],"Transportation":[["Car
Payment":0.0],["Car Insurance":0.0],["Roadside Insurance":0.0]],"Other Expenses":
[["Health Insurance":0.0],["Life Insurance":0.0],["Disability Insurance":0.0],
["Student Loans":0.0],["Cell Phone":0.0],["Other":0.0]]]
你可以做这样的事情吗?
为什么不设置tag
每个标签和textFiled喜欢跟随你cellForRowAtIndexPath
textField.tag = indexPath.row
并在委托textFieldDidBeganEditing
或在必要时使用标记来获取cell
,然后使用以下label
let indexpath = NSIndexPath(forRow: textField.tag, inSection: 0)
let currentCell = tblTest.cellForRowAtIndexPath(indexpath) as! youCellClass
然后使用currentCell
您可以访问它的label
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.