ios 如何快速使用 Textfield 制作 UITableview?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31736884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make UITableview with Textfield in swift?
提问by Zablah
I want to make a table view with textfields in each cell,
我想在每个单元格中制作一个带有文本字段的表格视图,
I have a custom class in a swift file:
我在 swift 文件中有一个自定义类:
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
}
}
Then in my ViewController I have
然后在我的 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
}
That works well:
这很好用:
But when the user enters text in the textfield and press the button I want to store the strings of each textfield in an array. I have tried with
但是当用户在文本字段中输入文本并按下按钮时,我想将每个文本字段的字符串存储在一个数组中。我试过
text = cell.textField.text
println(text)
But it prints nothing like if it was empty
但它不会打印任何东西,就像它是空的一样
How can I make it work?
我怎样才能让它工作?
回答by Fred Faust
In your view controller become a UITextFieldDelegate
在您的视图控制器中成为 UITextFieldDelegate
View Controller
视图控制器
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)
}
}
This will always append the data from the textField to the allCellsText array.
这将始终将 textField 中的数据附加到 allCellsText 数组。
回答by knownUnknown
create a variable
创建一个变量
var arrayTextField = [UITextField]()
after this in your func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{}
add
在此之后func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{}
添加
self.arrayTextField.append(textfield)
before returning cell, after this to display the values using
在返回单元格之前,在此之后使用显示值
for textvalue in arrayTextField{
println(textvalue.text)
}
回答by user2680768
this method is init the cell,u have not model to save this datas,so
这个方法是初始化单元格,你没有模型来保存这些数据,所以
text = cell.textfield.text
is nothing!
you can init var textString
in viewcontroller,an inherit UITextFieldDelegate
没什么!您可以var textString
在视图控制器中初始化,继承 UITextFieldDelegate
optional func textFieldDidEndEditing(_ textField: UITextField)
{
textString = _textField.text
}
optional func textFieldShouldReturn(_ textField: UITextField) -> Bool{
return true
}
and cell.textField.text = textString
和 cell.textField.text = textString
回答by justdan0227
So here is a way to do it using the suggestion of having a textfield array. It uses the tag of the textfield to ensure it does not get added multiple times when you scroll.
所以这是一种使用文本字段数组的建议来做到这一点的方法。它使用文本字段的标签来确保滚动时不会多次添加它。
// 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
}
// And then you cam get the values when you want to save with where tag - 100 will be the array index value to update
// 然后你可以在你想用 where 标签保存时获取值 - 100 将是要更新的数组索引值
for textvalue in arrayTextField{ print(textvalue.text) }
用于 arrayTextField 中的文本值{ print(textvalue.text) }