如何从Custom UITableViewCell的ViewController获取值?

问题描述:

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! } –

这里有一个简单的解决方案。它可以用于任何数量的单元。

你需要做的是通过迭代细胞,并计算出,如果特定的细胞是抱着文本框为空。现在的问题是你将如何遍历单元格,有没有代表呢?答案是否定的

您必须手动构建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 
     } 
    } 
} 

有这说明了一切评论。希望这可以帮助。

创建你的类,你必须在按键动作的布尔变量

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中的表视图有文字,它会打印假的,否则,如果所有的文本字段中只有一个文本字段没有文本,将打印真正