如何在 swift 中使用 Textfield 制作 UITableview?
How to make UITableview with Textfield in swift?
我想制作一个 table 视图,每个单元格中都有文本字段,
我在 swift 文件中有一个自定义 class:
import UIKit
public class TextInputTableViewCell: UITableViewCell{
@IBOutlet weak var textField: UITextField!
public func configure(#text: String?, placeholder: String) {
textField.text = text
textField.placeholder = placeholder
textField.accessibilityValue = text
textField.accessibilityLabel = placeholder
}
}
然后在我的 ViewController 我有
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier("TextInputCell") as! TextInputTableViewCell
cell.configure(text: "", placeholder: "Enter some text!")
text = cell.textField.text
return cell
}
效果不错:
但是当用户在文本字段中输入文本并按下按钮时,我想将每个文本字段的字符串存储在一个数组中。
我试过
text = cell.textField.text
println(text)
但是如果它是空的,它什么也不会打印
我怎样才能让它发挥作用?
这个方法是初始化单元格,你没有模型来保存这个数据,所以
text = cell.textfield.text
没什么!
你可以在 viewcontroller 中初始化 var textString
,继承 UITextFieldDelegate
optional func textFieldDidEndEditing(_ textField: UITextField)
{
textString = _textField.text
}
optional func textFieldShouldReturn(_ textField: UITextField) -> Bool{
return true
}
和cell.textField.text = textString
在您的视图控制器中成为 UITextFieldDelegate
视图控制器
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate {
var allCellsText = [String]()
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomTableViewCell
cell.theField.delegate = self // theField is your IBOutlet UITextfield in your custom cell
cell.theField.text = "Test"
return cell
}
func textFieldDidEndEditing(textField: UITextField) {
allCellsText.append(textField.text!)
println(allCellsText)
}
}
这将始终将 textField 中的数据附加到 allCellsText 数组。
创建一个变量
var arrayTextField = [UITextField]()
在此之后在您的 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{}
添加
self.arrayTextField.append(textfield)
在返回单元格之前,在此之后使用
显示值
for textvalue in arrayTextField{
println(textvalue.text)
}
这里有一种方法可以使用具有文本字段数组的建议。它使用文本字段的标签来确保它不会在您滚动时被多次添加。
// Assumes that you have a label and a textfield as a part of you tableViewCell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? BodyStatsTableViewCell else {
return UITableViewCell()
}
cell.rowLabel.text = bodyTableArray[indexPath.row].rowTitle
let tagValue = indexPath.row + 100
var newRow = true
for textvalue in arrayTextField{
if textvalue.tag == tagValue {
newRow = false
}
}
if newRow {
cell.rowTextField.tag = tagValue
self.arrayTextField.append(cell.rowTextField)
cell.rowTextField.text = bodyTableArray[indexPath.row].rowValue
}
return cell
}
// 然后当你想用 where 标签保存时你会得到值 - 100 将是要更新的数组索引值
对于 arrayTextField 中的文本值{
打印(textvalue.text)
}
我想制作一个 table 视图,每个单元格中都有文本字段,
我在 swift 文件中有一个自定义 class:
import UIKit
public class TextInputTableViewCell: UITableViewCell{
@IBOutlet weak var textField: UITextField!
public func configure(#text: String?, placeholder: String) {
textField.text = text
textField.placeholder = placeholder
textField.accessibilityValue = text
textField.accessibilityLabel = placeholder
}
}
然后在我的 ViewController 我有
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier("TextInputCell") as! TextInputTableViewCell
cell.configure(text: "", placeholder: "Enter some text!")
text = cell.textField.text
return cell
}
效果不错:
但是当用户在文本字段中输入文本并按下按钮时,我想将每个文本字段的字符串存储在一个数组中。 我试过
text = cell.textField.text
println(text)
但是如果它是空的,它什么也不会打印
我怎样才能让它发挥作用?
这个方法是初始化单元格,你没有模型来保存这个数据,所以
text = cell.textfield.text
没什么!
你可以在 viewcontroller 中初始化 var textString
,继承 UITextFieldDelegate
optional func textFieldDidEndEditing(_ textField: UITextField)
{
textString = _textField.text
}
optional func textFieldShouldReturn(_ textField: UITextField) -> Bool{
return true
}
和cell.textField.text = textString
在您的视图控制器中成为 UITextFieldDelegate
视图控制器
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate {
var allCellsText = [String]()
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomTableViewCell
cell.theField.delegate = self // theField is your IBOutlet UITextfield in your custom cell
cell.theField.text = "Test"
return cell
}
func textFieldDidEndEditing(textField: UITextField) {
allCellsText.append(textField.text!)
println(allCellsText)
}
}
这将始终将 textField 中的数据附加到 allCellsText 数组。
创建一个变量
var arrayTextField = [UITextField]()
在此之后在您的 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{}
添加
self.arrayTextField.append(textfield)
在返回单元格之前,在此之后使用
显示值for textvalue in arrayTextField{
println(textvalue.text)
}
这里有一种方法可以使用具有文本字段数组的建议。它使用文本字段的标签来确保它不会在您滚动时被多次添加。
// Assumes that you have a label and a textfield as a part of you tableViewCell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? BodyStatsTableViewCell else {
return UITableViewCell()
}
cell.rowLabel.text = bodyTableArray[indexPath.row].rowTitle
let tagValue = indexPath.row + 100
var newRow = true
for textvalue in arrayTextField{
if textvalue.tag == tagValue {
newRow = false
}
}
if newRow {
cell.rowTextField.tag = tagValue
self.arrayTextField.append(cell.rowTextField)
cell.rowTextField.text = bodyTableArray[indexPath.row].rowValue
}
return cell
}
// 然后当你想用 where 标签保存时你会得到值 - 100 将是要更新的数组索引值
对于 arrayTextField 中的文本值{ 打印(textvalue.text) }