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

时间:2017-12-07 09:10:56

标签: ios swift uicollectionview uitextfield uicollectionviewcell

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

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

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

2 个答案:

答案 0 :(得分: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.")
    }
}

答案 1 :(得分:1)

要确定UITextField内的UICollectionViewCell是否为空,您需要在单击按钮时传递索引位置。要传递索引位置,您可以使用标记。通过使用标记,您可以获得单击的索引位置。获取位置后,可以使用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")
  }
}

希望这会对你有所帮助

相关问题