2016-08-05 74 views
2

Please look at this image如何從Custom UITableViewCell的ViewController獲取值?

我對的UITableViewCell及其IBOutlets 2個UITextFields連接稱爲「CustomCell.swift」的自定義的UITableViewCell類。 EnterControl按鈕在ViewController的UIView上,它的IBAction在UIViewController類中被稱爲「ViewController」。

在輸入按鈕,我想看看這兩個文本框是空的點擊。我該怎麼做?請幫助

+0

可以這樣做@IBAction func enquireButton(sender:AnyObject)let cell = tableView.dequeueReusableCellWithIdentifier(「Guest Details」)as! GuestDetailsTableViewCell;讓fullName1 = cell.fullNameTextField.text! } –

回答

0

這裏有一個簡單的解決方案。它可以用於任何數量的單元。

你需要做的是通過迭代細胞,並計算出,如果特定的細胞是抱着文本框爲空。現在的問題是你將如何遍歷單元格,有沒有代表呢?答案是否定的

您必須手動構建indexPaths來從表的單元格。

這是一個簡單的步驟。你的設置是非常正確的。你應該在你的ViewController中有一個tableview。所以,桌面視圖的IBOutlet應該在那裏。我將我的TableView命名爲「myTableView」。而textField的Outlet應該位於TableViewCell中,這也是正確的。最後,Enter按鈕的操作方法應該在視圖控制器中。

確保您正確連接所有插座。

這裏是來樣定製TableViewCell -

import UIKit 

class CustomTableViewCell: UITableViewCell { 

    @IBOutlet weak var internalTextField : UITextField! 

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

} 

而現在只是去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 
     } 
    } 
} 

有這說明了一切評論。希望這可以幫助。

0

創建你的類,你必須在按鍵動作的布爾變量

var isTextFieldTextEmpty: Bool! 

然後在表視圖數據源方法的cellForRowAtIndexPath添加

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

那麼的IBAction爲你(回車)按鈕添加

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

如果所有單元格中的所有文本字段o F中的表視圖有文字,它會打印假的,否則,如果所有的文本字段中只有一個文本字段沒有文本,將打印真正