簡體   English   中英

Swift:跳到CollectionView中的下一個Textfield

[英]Swift: Jump to next Textfield in a CollectionView

我試圖通過單擊回車按鈕從一個單元格中的一個文本字段跳轉到CollectionView的下一個單元格中的TextField,但是我無法使其正常工作。

通過為每個Textfield賦予相應的indexPath作為Tag,在單元格創建后,我已經為所有Textfield分配了一個單獨的Tag:

// make a cell for each cell index path
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    // get a reference to our storyboard cell
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell

    // Use the outlet in our custom class to get a reference to the UILabel in the cell
    cell.myLabel.text = self.items[indexPath.item]
    cell.myTextField.tag = indexPath.row
    cell.backgroundColor = UIColor(red:0.94, green:0.94, blue:0.94, alpha:1.0) // make cell more visible in our example project

    // Change shape of cells
    cell.layer.cornerRadius = 8

    return cell
}

現在,我的意圖是,使用下面的代碼到達下一個Textfield(如果有):

extension ViewController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    // Try to find next responder
    print(textField.tag) // Test if chosen textfield has individual tag
    if let nextField = textField.superview?.viewWithTag(textField.tag + 1) as? UITextField {
        nextField.becomeFirstResponder()
    } else {
        // Not found, so remove keyboard.
        textField.resignFirstResponder()
    }
    // Do not add a line break
    return false
}

}

但是每當我單擊回車按鈕時,nextField始終為nil,否則將執行else情況並且鍵盤消失。 我的猜測是,nextField在其他TextField中的位置不正確,這就是為什么找不到它們的原因(每個單元格只有一個Textfield,而在ViewController中沒有TextFields直接)。

由於我對編程一般來說還很陌生,所以我不知道如何解決此問題。

if let nextField = textField.superview?.viewWithTag(textField.tag + 1) as? UITextField

在這段代碼中,textField.superview是當前單元格。沒有視圖的標簽等於textField.tag +1。您應該先獲取下一個單元格,然后獲取textFiled。 像這樣更改代碼

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
// Try to find next responder 
    print(textField.tag)
// Test if chosen textfield has individual tag
    let nextCell = self.collectionView?.cellForItem(at: IndexPath.init(row: textField.tag + 1, section: 0))
if let nextField = nextCell.viewWithTag(textField.tag + 1) as? UITextField {
    nextField.becomeFirstResponder()
} else {
    // Not found, so remove keyboard.
    textField.resignFirstResponder()
}
    // Do not add a line break
return false
}

您錯誤地認為集合視圖是每個文本字段的超級視圖。 這不是真的 文本字段的超級視圖實際上是集合視圖單元格的contentView ,而后者又是集合視圖單元格的某個深層子視圖,該子視圖最終是集合視圖的子視圖。

當下一個文本字段甚至不可見時,您還會嘗試移動到下一個文本字段。

您需要采用其他方法。 在較高的層次上,您需要在textFieldShouldReturn方法中執行以下操作:

  1. 確定文本字段的單元格的索引路徑。
  2. 計算下一個索引路徑。
  3. 確保下一個索引路徑的單元格在視圖中。
  4. 將下一個單元格的文本字段設為第一響應者。

對於步驟1,您需要將文本字段中的點轉換為集合視圖中的點。 然后,使用集合視圖方法indexPathForItem(at:)獲取當前文本字段的單元格的索引路徑。

對於步驟2,這取決於您的布局。 只需增加item 可能增加該section 確保檢查您是否位於最后一個單元格。

對於步驟3,根據需要滾動收集視圖。

對於第4步,使用cellForItem(at:)集合視圖方法傳遞第2步中的新索引路徑。這將為您提供下一個單元格。 然后訪問其文本字段並使其成為第一響應者。

暫無
暫無

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

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