我正在构建一种数据库,因此我创建了一个带有自定义单元格的 TableView ,以便用户可以将请求的数据插入到适当的文本字段中。这是到目前为止的样子。
这是我为实现此目的而编写的代码。
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/