如何将文本添加到 tableViewCell
中?我拖了一个 textfield
和 button
,并在它们下面添加,设置 tableView
和 tableviewCell
。单元格的标识符是“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/