3

Please look at this image

I have two UITextFields on the UITableViewCell and their IBOutlets are connected in the custom UITableViewCell class called as "CustomCell.swift". The Enter button is there on the UIView of ViewController and its IBAction is there in the UIViewController class called as "ViewController".

On click of the Enter button I want to see if the two textFields are empty. How do I do it? Please help

Anirudha Mahale
  • 2,526
  • 3
  • 37
  • 57
  • Can I do something like this @IBAction func enquireButton(sender: AnyObject) { let cell = tableView.dequeueReusableCellWithIdentifier("Guest Details") as! GuestDetailsTableViewCell; let fullName1 = cell.fullNameTextField.text!; } – Anirudha Mahale Aug 05 '16 at 08:00

2 Answers2

0

create a Bool variable in your class where you have the button action

var isTextFieldTextEmpty: Bool!

then in your table view dataSource method cellForRowAtIndexPath add

if myCell.myTextField.text?.isEmpty == true {
        self.isTextFieldTextEmpty = true
    } else {
        self.isTextFieldTextEmpty = false
    }

then in the IBAction of your (Enter) button add

self.myTableView.reloadData()
self.myTableView.layoutIfNeeded()
print(self.isTextFieldTextEmpty) 

if all text fields in all cells of the table view have text, it will print false, else if only one text fields among all the text fields has no text, it will print true

Sameh Salama
  • 765
  • 1
  • 7
  • 17
-1

Here is a simple solution. It will work for any number of cells.

What you need to do is iterate through the cells and figure out if the textField that particular cell is holding is empty or not. Now the question is how will you iterate through the cells, is there any delegate for that? The answer is No.

You have to manually construct the indexPaths to get the cells from the Table.

Here is a simple walk through. Your set up is quite right. You should have a tableview in your ViewController. So, the IBOutlet of the tableview should be there. I named my TableView "myTableView". And the textField's Outlet should be inside the TableViewCell which is also right. At the end the action method for the Enter button should be in the view controller.

Make sure, you properly connect all the outlets.

Here is the sample custom TableViewCell -

import UIKit

class CustomTableViewCell: UITableViewCell {

    @IBOutlet weak var internalTextField : UITextField!

    override func awakeFromNib() {
        super.awakeFromNib()
    }

}

And now just go to the ViewController.swift-

import UIKit

class ViewController: UIViewController, UITableViewDataSource  {

    @IBOutlet weak var myTableView : UITableView!

    var numberOfCells = 2  //you can update it to be any number

    override func viewDidLoad() {
        super.viewDidLoad()
        self.myTableView.dataSource! = self  //assign the delegate
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return numberOfCells
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell : CustomTableViewCell = tableView.dequeueReusableCellWithIdentifier("customCell", forIndexPath: indexPath) as! CustomTableViewCell
        return cell;
    }

    @IBAction func pressedEnter(){

        var row  = 0

        while row < numberOfCells { //iterate through the tableview's cells

            let indexPath : NSIndexPath = NSIndexPath(forRow: row, inSection: 0) //Create the indexpath to get the cell
            let cell : CustomTableViewCell = self.myTableView.cellForRowAtIndexPath(indexPath) as! CustomTableViewCell

            if cell.internalTextField.text!.isEmpty{
                print("TextField is Cell \(row) is Empty")
            }
            else{
                print("TextField is Cell \(row) is NOT Empty")
            }
            row += 1
        }
    }
}

There are comments which explains everything. Hope this helps.

Natasha
  • 6,651
  • 3
  • 36
  • 58
  • getting "Unexpectedly found nil while unwrapping an Optional value" at -> let cell : CustomTableViewCell = self.myTableView.cellForRowAtIndexPath(indexPath) as! CustomTableViewCell . Could someone please help resolve this error? – Arjun Oct 07 '19 at 15:13