簡體   English   中英

UITableView中的UITextField不會成為第一響應者

[英]UITextField in UITableView not becoming first responder

我有大約20行的UITableView ,每個行包含一個UITextField 第一次點擊文本字段將打開鍵盤,我准備編輯此文本字段。 如果我點擊下一個文本字段(注意始終顯示鍵盤),鍵盤仍會顯示,但是藍色光標不在新的文本字段中,並且我無法輸入任何文本。 但是,如果我再次點擊另一個文本字段,它就可以正常工作。 此行為交替發生,一次有效,另一次無效。

不管是否可以編輯,總是調用委托方法textFieldShouldBeginEditing(_:) 僅在編輯工作時調用委托方法textFieldDidBeginEditing(_:)

這是cellForRowAt的代碼

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "TextFieldCell")!
    let titleLabel = cell.viewWithTag(1) as! UILabel
    let contentTextField = cell.viewWithTag(2) as! FixableTextField
    contentTextField.delegate = self
    contentTextField.inputAccessoryView = doneToolbar

    // Enable/disable editing for text fields
    if isEditing {
        contentTextField.enableEditing()
    } else {
        contentTextField.disableEditing()
    }

    // Present Profile Data
    if profileUpdateBuffer != nil {

        switch indexPath.row {
        case 0:
            titleLabel.text = "Count"
            contentTextField.text = "\(profileUpdateBuffer!.count)"
            contentTextField.purposeID = "count"
            contentTextField.keyboardType = .numberPad

        case 1:
            titleLabel.text = "City"
            contentTextField.text = "\(profileUpdateBuffer!.city)"
            contentTextField.purposeID = "city"
            contentTextField.keyboardType = .default

        // ...

        case 20:
            titleLabel.text = "Name"
            contentTextField.text = "\(profileUpdateBuffer!.name)"
            contentTextField.purposeID = "name"
            contentTextField.keyboardType = .default

        default:
            titleLabel.text = ""
            contentTextField.text = ""
        }

        return cell
    }

    // No data available -> show info in first row
    else {
        if indexPath.row == 0 {
            titleLabel.text = "No data"
            contentTextField.text = "No data"
        }
        else {
            titleLabel.text = ""
            contentTextField.text = ""
        }
        return cell
    }
}

enableEditing()disableEditing()方法來自FixableTextField類。 我可以看到文本字段始終處於啟用狀態,因為我可以看到文本字段邊框

// Extract from FixableTextField class
func enableEditing() {
    self.isEnabled = true
    self.borderStyle = .roundedRect
}

func disableEditing() {
    self.isEnabled = false
    self.borderStyle = .none
}

UITextField代碼

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {

    // Delete empty field indicator "-"
    if textField.text == "-" {
        textField.text = ""
    }

    //Move profileTable's contentView to correct position
    if textField is FixableTextField {
        let path = IndexPath(row: rowMap[(textField as! FixableTextField).purposeID]!, section: 0)
        moveContentViewUp(indexPath: path)
    }
    return true
}



func textFieldDidEndEditing(_ textField: UITextField) {

    // Save new value to profileUpdateBuffer
    do {
        try self.profileUpdateBuffer?.setProperty(value: textField.text!, key: (textField as! FixableTextField).purposeID)
    } catch ProfileError.PropertySettingWrongType {
        let falseInputAlert = UIAlertController(title: "False Input", message: "The input for this field is not valid.", preferredStyle: .alert)
        falseInputAlert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
        self.present(falseInputAlert, animated: true, completion: nil)
    } catch {
        print("Error when trying to set property for profileUpdateBuffer in ProfileViewController")
    }

    // Display new data in table
    profileTable.reloadData()
}

從類ProfileData setProperty方法中提取。 profileUpdateBuffer的類型為ProfileData

func setProperty(value:String, key:String) throws {
    switch key {
    case "count":
        count = value

    case "city":
        count = value

    // ...

    case "name":
        name = value

    default:
        throw ProfileError.PropertySettingWrongType
    }
}

我編寫了一個小程序來模仿您描述的行為。 看來問題是由在textFieldDidEndEditing(_:)末尾重新加載表視圖數據引起的:

func textFieldDidEndEditing(_ textField: UITextField) {

    // Save new value to profileUpdateBuffer
    do {
        try self.profileUpdateBuffer?.setProperty(value: textField.text!, key: (textField as! FixableTextField).purposeID)
    } catch ProfileError.PropertySettingWrongType {
        let falseInputAlert = UIAlertController(title: "False Input", message: "The input for this field is not valid.", preferredStyle: .alert)
        falseInputAlert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
        self.present(falseInputAlert, animated: true, completion: nil)
    } catch {
        print("Error when trying to set property for profileUpdateBuffer in ProfileViewController")
    }

    // Display new data in table
    profileTable.reloadData()
}

嘗試刪除profileTable.reloadData()以便進行實驗,以確認問題的根本原因(是的,其他單元格將不會更新)。

解決此問題的一種方法是利用textFieldDidEndEditing(_:) visibleCells上的直接單元更新。 我看到profileUpdateBuffer? 是您的數據模型。 如果它們位於表視圖的可見單元格屬性中,則只需從模型中手動更新單元格的titleLabel和textField屬性即可。

如果要相應地調整單元格的大小,請結合使用beginUpdates() / endUpdates()將AutoLayout和UITableViewAutomaticDimension用於表視圖的行高。

有關如何實現直接單元格操作和/或動態單元格大小更新而又不失去鍵盤焦點的更多詳細信息, 請檢查已回答的問題的可接受答案

希望這會有所幫助!

我相信問題是您調用profileTable.reloadData()...您可能應該只重新加載一個單元格。 也許使用舊的TextField作為參數調用 textFieldDidEndEditing( :),然后使用新的TextField 調用textFieldShouldBeginEditing( :)。 問題是您刷新了textFieldDidEndEditing( :)末尾的所有單元格,這意味着系統作為參數傳遞給TextFieldShouldBeginEditing:)的TextField不一定與相應單元格所包含的相同...可能是那些新的按鍵被發送到屬於可重用單元隊列中的單元的TextField,即不可見,但仍存在於內存中的某個位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM