ios - 使用自定义表格 View 单元格从 UITextField 获取数据

标签 ios swift uitableview xcode8

我正在构建一种数据库,因此我创建了一个带有自定义单元格的 TableView ,以便用户可以将请求的数据插入到适当的文本字段中。这是到目前为止的样子。

Database app

这是我为实现此目的而编写的代码。

class customCell1: UITableViewCell {

    @IBOutlet weak var cc1TextField1: UITextField!
    @IBOutlet weak var cc1TextField2: UITextField!
    @IBOutlet weak var cc1TextField3: UITextField!

    @IBOutlet weak var cc1Label1: UILabel!
    @IBOutlet weak var cc1Label2: UILabel!
    @IBOutlet weak var cc1Label3: UILabel!

    @IBOutlet weak var cc1MainLabel: UILabel!

}

简而言之,在 Swift 文件中,我创建了一个类,将其命名为 customCell1,而在 ViewController 中,我将其命名为 InputDataController,我插入了以下几行代码,其中 inputDataString 是有关必须分配给每行中每个标签的不同标题的字符串向量。

func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return inputDataString.count

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

            let cell1 = tableView.dequeueReusableCell(withIdentifier: "myCell1", for: indexPath) as? customCell1

            cell1?.cc1MainLabel.text = inputDataString[indexPath.row]  

            cell1?.cc1Label1.text = "Fabrics"
            cell1?.cc1Label2.text = "Fittings"
            cell1?.cc1Label3.text = "Furniture"

            cell1?.cc1TextField1.placeholder = "Insert value here"
            cell1?.cc1TextField2.placeholder = "Insert value here"
            cell1?.cc1TextField3.placeholder = "Insert value here"

            return cell1!

        }

现在我的问题是,一旦用户在文本字段中添加所有这些值并单击我在导航栏上添加的保存按钮,如何从各个文本字段获取所有数据并将它们分配给变量(s),以便我可以使用这些值进行一些计算。而且,我应该在哪里添加这些代码行才能一切正常。

最佳答案

您可以通过导航栏中按钮的for操作来获取所有数据:

let fabrics = [String]()
let fittings = [String]()
let furnitures = [String]()
for index in 0 ... inputDataString.count-1 {
    fabrics.append((tableView.cellForRow(at: index) as! customCell1).cc1TextField1.text)
    fittings.append((tableView.cellForRow(at: index) as! customCell1).cc1TextField2.text)
    furnitures.append((tableView.cellForRow(at: index) as! customCell1).cc1TextField3.text)
}

关于ios - 使用自定义表格 View 单元格从 UITextField 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49930838/

相关文章:

c# - 绑定(bind)的 Xamarin.iOS 库子类在模拟器上崩溃

ios - 如何解决 uitableviewcell 重用的自定义子类的问题?

ios - 在 UILabel 上调用 sizeToFit() 并不总能在自定义 tableview 单元格中产生正确的结果

iphone - 关于内购的问题 : auto-renewable subscriptions

iphone - 使用 nsdate 比较两个日期

swift - 如何快速获取正确的 'type '

php - 无法在 Swift 应用程序上解析服务器 PHP

ios - 我如何像在 Objective C 宏中声明那样全局声明变量或函数

ios - Swift 静态表格单元格页脚文本

iphone - UITableViewCell无法正确更新