【问题标题】:How to make UITableview with Textfield in swift?如何在 swift 中使用 Textfield 制作 UITableview?
【发布时间】:2015-10-22 14:01:56
【问题描述】:

我想在每个单元格中创建一个带有文本字段的表格视图,

我在 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
    }
}

然后在我的 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)

但是如果它是空的,它不会打印任何东西

我怎样才能让它工作?

【问题讨论】:

标签: ios swift uitableview uitextfield cell


【解决方案1】:

在您的视图控制器中成为 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)
    }
}

这将始终将文本字段中的数据附加到 allCellsText 数组。

【讨论】:

  • 这行得通!但是当按下按钮时,如何将 textfield.text 附加到数组中?我尝试将:allCellsText.append(textField.text) println(allCellsText) 放在 buttonPressed 函数中,但它不能识别文本字段
  • 这不能与 textField 委托一起使用,但您可以使用按钮对数据设置某种类型的条件,或者将数据复制到不同的数组。
  • @thefredelement 在class ViewController 中我创建了一个TextInputTableViewCell 实例,在ViewDidAppear 中,我试图将UserDefaults 中的数据分配给TextInputTableViewCell 中的var textField,但分配失败,意外发现nil 而展开可选值
  • 但这不会在调用textFieldDidEndEditing 时继续附加到allCellsText 吗?
【解决方案2】:

所以这是一种使用文本字段数组的建议的方法。它使用文本字段的标签来确保滚动时不会多次添加它。

// 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 中的文本值{ 打印(文本值。文本) }

【讨论】:

  • 滚动后,arrayTextField 中的 UITextField 可能与 UI 中可见的不同。此外,该问题的其他解决方案也可以正常工作,而无需求助于这种黑客。
  • @fishinear,谢谢,不知道我要去哪里
【解决方案3】:

创建一个变量

var arrayTextField = [UITextField]()

在您的func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{} 之后添加

self.arrayTextField.append(textfield)

在返回单元格之前,在此之后使用显示值

for textvalue in arrayTextField{
        println(textvalue.text)
    }

【讨论】:

  • 这不会导致可能被回收的表格行出现问题,还是即使您滚动到视图之外它也会保留单元格的内存位置?
  • 这是个好主意,但是正如@JoseRamirez 指出的那样,如果您滚动视图,您的 cellForRowAtIndexPath 将附加另一个文本视图。
【解决方案4】:

这个方法是初始化单元格,你没有模型来保存这些数据,所以

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-05
    • 1970-01-01
    • 1970-01-01
    • 2012-05-04
    • 1970-01-01
    • 2021-06-11
    • 2018-08-21
    • 2012-08-22
    相关资源
    最近更新 更多