私は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.")
}
}
問題は、テキストフィールドが空であるかどうか私はチェックするたびに、それはそれはないのですが、それが空であるのを言うことです。
の追加を助けるない特定のインデックス位置を見つけることができますあなたのifステートメントの直前の** ** print( "The text is:\(text)")**これは間違っているものを確かに確認します – Yitzchak
AHH HH - あなたは正しくセルを使用していません。私は1分後に回答を投稿します – Yitzchak