添加自定义单元格标签和TextField文本到字典数组
我有一个自定义tableview单元格与UILabel和UITextField。我想将带有标签的textField中的数据附加到字典数组([label:textfield])。我可以使用textFieldDidEndEditing获取textField数据,但我不确定如何获取同一文本字段的单元格标签。我想我会需要访问该单元格的indexPath。我试着发送一个通知给DidSelectRow,但这看起来太复杂了。添加自定义单元格标签和TextField文本到字典数组
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
这很好,但我并不是真的想惹这个项目的标签。在评论中提出的可见细胞方向似乎是一个好方向,但我仍然遇到麻烦。 –
第二个选项可以是你的UILabel和文本字段的子类 –
您可以在tableView.visibleCells()中为单元格执行此操作! [UITableViewCell] //在这里对单元格进行操作}' – iphonic
这使我可以看到每个可见单元格中的每个元素,但它不会让我更新并将这些单元格的值附加到我的字典中(请参阅更新) –
不允许你更新?你有什么类型的对象,如果它的'NSArray或NSDictionary'你需要使用'NSMutableArray或者NSMutableDictionary',并且改变了值后重新加载表。 – iphonic