将自定义单元格标签和TextField文本附加到字典数组

时间:2016-11-28 05:34:49

标签: ios swift uitableview uitextfield uitextfielddelegate

我有一个带有UILabel和UITextField的自定义tableView单元格。我想将textField中带有label的数据附加到一个字典数组([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]]]

1 个答案:

答案 0 :(得分: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

相关问题