ios - 如何使用文本字段和按钮将文本添加到每个单元格中?

标签 ios swift uitableview uitextfield

如何将文本添加到 tableViewCell 中?我拖了一个 textfieldbutton,并在它们下面添加,设置 tableViewtableviewCell。单元格的标识符是“Cell”。这是我到目前为止所做的代码。理想情况下,我想将在 textfield 中键入的文本添加到单元格中,例如,如果我添加我的名字,它将被添加,如果下一个人添加她的名字,她的名字也会出现在细胞。

@IBOutlet weak var myTableView: UITableView!
@IBOutlet weak var textField: UITextField!

var stringArray = [String]()
@IBOutlet weak var AddBUtton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    textField.delegate = self
}

func textFieldDidBeginEditing(textField: UITextField) {
}
func textFieldDidEndEditing(textField: UITextField) {
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
    return true
}
func textFieldShouldClear(textField: UITextField) -> Bool {
    return true
}
func textFieldShouldEndEditing(textField: UITextField) -> Bool {
    return true
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellIdentifier = "Cell"
    let cell = self.myTableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! UITableViewCell 
    return cell
}

最佳答案

只需尝试设置 UITableViewCell 的 textLabel

func textFieldShouldReturn(textField: UITextField) -> Bool {
    stringArray.append(textField.text)
    textField.text = nil
    textField.resignFirstResponder()
    table.reloadData() // if possible just reload or insert only the cell
    return true
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellIdentifier = "Cell"
    let cell = self.myTableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! UITableViewCell 
    cell.textLabel.text = stringArray[indexPath.row]
    return cell
}

关于ios - 如何使用文本字段和按钮将文本添加到每个单元格中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34305381/

相关文章:

ios - ARKit - 如何创建旋转对象手势功能?

ios - Swift xcode 错误 : load controller is not allowed and autoresize not equal to views window

objective-c - UITableView 层上的内部阴影?

ios - 无法离开行操作按钮

ios - 将行添加到 UITableView - NSInternalInconsistency 错误

ios - UIDatePicker 没有正确响应

ios - 多行 UILabel

swift - 在 ScrollView 中动态隐藏状态栏时滞后/屏幕卡住 (Swift 3)

ios - 函数无法正确执行(Swift 3)

ios - 在项目和共享扩展之间共享数据