【问题标题】:How do I get the values from Custom UITableViewCell to ViewController?如何从自定义 UITableViewCell 获取值到 ViewController?
【发布时间】:2023-04-11 07:28:01
【问题描述】:

我在 UITableViewCell 上有两个 UITextField,它们的 IBOutlets 连接在称为“CustomCell.swift”的自定义 UITableViewCell 类中。 Enter 按钮位于 ViewController 的 UIView 上,其 IBAction 位于称为“ViewController”的 UIViewController 类中。

单击 Enter 按钮时,我想查看两个文本字段是否为空。我该怎么做?请帮忙

【问题讨论】:

  • 我可以做这样的事情吗@IBAction func enquireButton(sender: AnyObject) { let cell = tableView.dequeueReusableCellWithIdentifier("Guest Details") as! GuestDetailsTableViewCell;让 fullName1 = cell.fullNameTextField.text!; }

标签: uitableview uibutton swift2 uitextfield


【解决方案1】:

在您有按钮操作的类中创建一个 Bool 变量

var isTextFieldTextEmpty: Bool!

然后在你的table view dataSource方法中添加cellForRowAtIndexPath

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

然后在您的 (Enter) 按钮的 IBAction 中添加

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

如果表格视图的所有单元格中的所有文本字段都有文本,则打印false,否则如果所有文本字段中只有一个文本字段没有文本,则打印true

【讨论】:

    【解决方案2】:

    这是一个简单的解决方案。它适用于任意数量的单元格。

    您需要做的是遍历单元格并确定特定单元格所包含的 textField 是否为空。现在的问题是您将如何遍历单元格,是否有任何代表?答案是否定的。

    您必须手动构建 indexPaths 才能从表格中获取单元格。

    这是一个简单的演练。你的设置非常正确。你的 ViewController 中应该有一个 tableview。因此,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
            }
        }
    }
    

    有 cmets 可以解释一切。希望这会有所帮助。

    【讨论】:

    • 在 -> let cell : CustomTableViewCell = self.myTableView.cellForRowAtIndexPath(indexPath) 处获得“在展开可选值时意外发现 nil”!自定义表视图单元。有人可以帮忙解决这个错误吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-03
    • 2016-05-23
    • 1970-01-01
    • 1970-01-01
    • 2012-07-01
    • 1970-01-01
    • 2016-05-17
    相关资源
    最近更新 更多