Swift: retrieving text from a UITextField in a custom UITableViewCell and putting it in an array

后端 未结 2 577
面向向阳花
面向向阳花 2020-12-01 17:08

I\'m making a very simple app where the user enters the number of people in the first Screen.

In the second screen it generates a number of UITableViewCell

相关标签:
2条回答
  • 2020-12-01 17:57

    There is a problem with your approach if the number of rows in your table exceeds the number that can fit on screen. In that case, the cells that scroll off-screen will be re-used, and the contents of the nameInput textField will be lost. If you can be sure that this will never happen, use the following code (in the method that handles button taps) to compose your array:

            var arrayOfNames : [String] = [String]()
            for var i = 0; i<self.arrayOfPeople.count; i++ {
                let indexPath = NSIndexPath(forRow:i, inSection:0)
                let cell : EditingCell? = self.tableView.cellForRowAtIndexPath(indexPath) as EditingCell?
                if let item = cell?.nameInput.text {
                    arrayOfNames.append(item)
                }
            }
            println("\(arrayOfNames)")
    

    Alternatively....

    However, if it is possible that cells will scroll off-screen, I suggest a different solution. Set the delegate for the nameInput text fields, and then use the delegate methods to grab the names as they are entered.

    First, add variables to your view controller, to hold the array and the row number of the text field currently being edited.

        var arrayOfNames : [String] = [String]()
        var rowBeingEdited : Int? = nil
    

    Then, in your cellForRowAtIndexPath method, add:

        cell.nameInput.text = "" // just in case cells are re-used, this clears the old value
        cell.nameInput.tag = indexPath.row
        cell.nameInput.delegate = self
    

    Then add two new functions, to catch when the text fields begin/end editing:

    func textFieldDidEndEditing(textField: UITextField) {
        let row = textField.tag
        if row >= arrayOfNames.count {
            for var addRow = arrayOfNames.count; addRow <= row; addRow++ {
                arrayOfNames.append("") // this adds blank rows in case the user skips rows
            }
        }
        arrayOfNames[row] = textField.text
        rowBeingEdited = nil
    }
    
    func textFieldDidBeginEditing(textField: UITextField) {
        rowBeingEdited = textField.tag
    }
    

    When the user taps the button, they might still be editing one of the names. To cater for this, add the following to the method that handles the button taps:

            if let row = rowBeingEdited {
                let indexPath = NSIndexPath(forRow:row, inSection:0)
                let cell : EditingTableViewCell? = self.tableView.cellForRowAtIndexPath(indexPath) as EditingTableViewCell?
                cell?.nameTextField.resignFirstResponder()
            }
    

    This forces the textField to complete editing, and hence trigger the didEndEditing method, thereby saving the text to the array.

    0 讨论(0)
  • 2020-12-01 18:11

    Here for new swift versions of answer

        var arrayOfNames : [String] = [String]()
        var i = 0
        while i < taskArrForRead.count {
            let indexPath = IndexPath(row: i, section: 0)
            let cell : taslakDuzenlemeCell? = self.tableView.cellForRow(at: indexPath) as! taslakDuzenlemeCell?
            if let item = cell?.taslakTextField.text {
                arrayOfNames.append(item)
            }
            i = i + 1
        }
        print("\(arrayOfNames)")
    
    0 讨论(0)
提交回复
热议问题