【发布时间】:2016-11-28 05:34:48
【问题描述】:
我有一个带有 UILabel 和 UITextField 的自定义 tableView 单元格。我想将带有标签的文本字段中的数据附加到字典数组([标签:文本字段])中。我可以使用 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]]]
【问题讨论】:
-
你可以这样做
for cell in tableView.visibleCells() as! [UITableViewCell] { //do something with the cell here.} -
这使我可以查看每个可见单元格中的每个元素是什么,但它不允许我更新这些单元格的值并将其附加到我的字典中(请参阅更新)
-
不让你更新?你有什么类型的对象,如果它是
NSArray or, NSDictionary,你需要使用NSMutableArray or, NSMutableDictionary,并在更改值后重新加载表。
标签: ios swift uitableview uitextfield uitextfielddelegate