检查 collectionView 中的文本字段是否为空

Check if text field in collectionView is empty

我有一个包含 3 个单元格的集合视图 (CollectionViewController)(每个单元格都有自己的 class)。在第一个单元格 (TextCell) 中,我有一个文本字段和一个按钮 (nextButton)。当我按下按钮时,我想检查文本字段是否为空。如果它不为空,我想切换到第二个单元格。

集合视图控制器:

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.")
    }
}

问题是每次我检查文本字段是否为空时,它都会说它是空的,尽管它不是。

首先 - 去掉下一行,不需要而且肯定是错误的!

let textCell = TextCell()

现在替换你的函数:

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.")
    }
}

判断UICollectionViewCell里面的UITextField是否为空,需要在点击按钮时传入索引位置。要传递索引位置,您可以使用标签。通过使用标签,您可以获得点击的索引位置。获取位置后,您可以使用 cellForItemAtIndexPath 从当前索引位置访问元素。您可以使用获取的单元格引用

找到特定索引位置 UITextField 是否为空

在你的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
       }
}

在你的ViewController

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")
  }
}

希望对您有所帮助