【问题标题】:Appending Custom Cell Label and TextField text to Array of Dictionary将自定义单元格标签和 TextField 文本附加到字典数组
【发布时间】: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


【解决方案1】:

你可以这样做吗?

为什么不为每个标签和 textFiled 设置 tag,就像在您的 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

【讨论】:

  • 这很好,但我真的不想弄乱这个项目的标签。 cmets 中呈现的可见细胞方向似乎是一个很好的方向,但我仍然遇到问题。
  • 第二个选项可能是子类你的 UILabel 和文本字段
猜你喜欢
  • 2020-12-28
  • 1970-01-01
  • 2019-05-13
  • 1970-01-01
  • 1970-01-01
  • 2020-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多