1

私は3つのセルを持つコレクションビュー(CollectionViewController)を持っています(各セルには独自のクラスがあります)。最初のセル(TextCell)には、テキストフィールドとボタン(nextButton)があります。ボタンを押すと、テキストフィールドが空であるかどうかをチェックしたいと思います。空でない場合は、2番目のセルに切り替える必要があります。 collectionViewのテキストフィールドが空であることを確認してください。

CollectionViewController:

let textCell = TextCell() 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: self.cellId, for: indexPath) 

    // TextCell 
    if indexPath.item == 0 { 

     let tCell = collectionView.dequeueReusableCell(withReuseIdentifier: textCellId, for: indexPath) 
      let nextButton = textCell.nextButton 
       nextButton.addTarget(self, action: #selector(handleNextButton), for: .touchUpInside) 
     return tCell 
    } 
    return cell 
} 


//Handle Action 

@objc func handleNextButton() { 
    let text = textCell.TextField.text 

    if text?.isEmpty == false { 
     scrollToIndex(menuIndex: 1) 
    } else { 
     print("Text field is empty.") 
    } 
} 

問題は、テキストフィールドが空であるかどうか私はチェックするたびに、それはそれはないのですが、それが空であるのを言うことです。

+0

の追加を助けるない特定のインデックス位置を見つけることができますあなたのifステートメントの直前の** ** print( "The text is:\(text)")**これは間違っているものを確かに確認します – Yitzchak

+0

AHH HH - あなたは正しくセルを使用していません。私は1分後に回答を投稿します – Yitzchak

答えて

1

ファースト - を削除する必要はありません。間違いは間違いです!

let textCell = TextCell() 

ことであなたのFUNCを置き換える:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    // TextCell 
    if indexPath.item == 0 { 

     let tCell = collectionView.dequeueReusableCell(withReuseIdentifier: textCellId, for: indexPath) as! TextCell 
      let nextButton = tCell.nextButton 
       nextButton.addTarget(self, action: #selector(handleNextButton), for: .touchUpInside) 
     return tCell 
    } 

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: self.cellId, for: indexPath) 
    return cell 
} 

をハンドラ

//Handle Action 
@objc func handleNextButton(sender: UIButton) { 
    let tCell = sender.superview as! TextCell 
    let text = tCell.TextField.text 

    if text?.isEmpty == false { 
     scrollToIndex(menuIndex: 1) 
    } else { 
     print("Text field is empty.") 
    } 
} 
+0

申し訳ありませんがあなたの答えを見たことがありません。しかし、ありがとう、私はそれを試みるでしょう。それが働いたら教えてください。時間がかかるかもしれない、私は現在急いでいます。 – Sam

+0

ああ、ええも完璧に動作します!ありがとうございました! – Sam

1

UICollectionViewCell内部UITextFieldが空であるかどうかを確認するには、次のようにする必要がありボタンをクリックするとインデックス位置を通過します。インデックス位置を渡すには、タグを使用できます。タグを使用すると、クリックされたインデックスの位置を取得できます。位置を取得したら、cellForItemAtIndexPathを使用して、現在のインデックス位置から要素にアクセスできます。あなたはUITextFieldのは、空であるか、あなたのViewControllerであなたのcellForItemAt

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

     if indexPath.item == 0{ 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YOUR_CELL_IDENTIFIER", for: indexPath) as! YOUR_UICOLLECTIONVIEW_CELL 
        cell.nextBtn.tag = indexPath.item 
        cell.nextBtn.addTarget(self, action: #selector(handleNextButton(_:)), for: .touchUpInside) 

      return cell 
     } else { 
       ///use your other cells here 
     } 
} 

でフェッチされたセル参照

を使用して

func handleNextButton(_ sender:UIButton){ 
    let indexPath = IndexPath(item:sender.tag,section:0) 
    let cell = YOUR_COLLECTIONVIEW.cellForItem(at: indexPath) as! YOUR_UICOLLECTIONVIEW_CELL 
    if cell.txtFld.text == "" { 
    print("TextField is empty") 
    } else { 
    print("TextField not empty") 
    } 
} 

ホープこれはあなたに

+0

ああ、完璧に感謝! – Sam

+0

あなたは歓迎です:) –

関連する問題