需要一点帮助
我的 NavigationController
中有一个添加按钮,它插入一个新的 CustomTableviewCell
,自定义 tableview 单元格由一个 TextField
组成。我已经能够成功添加新单元格,但我无法获取每个文本字段中的所有文本并将它们附加到数组。
var 数据:[String] = [""]
static var username = ""
@IBAction func addNewName(_ sender: Any) {
self.isHandlingAddNewUser = true
datas.insert(ViewController.username, at: 0)
tableView.beginUpdates()
tableView.insertRows(at: [IndexPath.init(row: 0, section: 0)], with: .automatic)
tableView.endUpdates()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return datas.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "UserCell", for: indexPath) as! UserInputCell
if let name = cell.usernameTextField.text{
ViewController.username = name
}
return cell
}
我错过了什么,我试着打印出datas
,都是空的
最佳答案
您不应更新 cellForRowAt
中的模型。那应该只填充单元格的文本字段的初始值。如果您想了解更改,则需要为单元格设置另一种机制,以便在文本字段更改时通知 View Controller 。
基本思路如下:
定义单元格可以通知 View Controller 更改的协议(protocol)(我称之为
UserInputCellDelegate
);cellForRowAt
应该仅使用模型中的值(您的datas
)更新单元格中的文本字段。它还将 View Controller 定义为单元格的委托(delegate)(以接收有关更改值的更新);当文本字段更新时(例如,为“编辑结束”连接
IBOutlet
),单元格将通过调用委托(delegate)方法通知 View Controller 该更改将更改通知 View Controller 。当 View Controller 调用其
didUpdate
时,它将相应地更新模型。
因此:
class ViewController: UITableViewController {
var datas = [String]() // start with empty array
@IBAction func didTapAddButton(_ sender: Any) {
let indexPath = IndexPath(row: 0, section:0)
datas.insert("", at: indexPath.row) // inserting default value (I'm inserting ""; you can insert whatever you want)
tableView.insertRows(at: [indexPath], with: .automatic)
}
}
// MARK: - UITableViewDataSource
extension ViewController {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return datas.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "UserCell", for: indexPath) as! UserInputCell
cell.delegate = self
cell.usernameTextField.text = datas[indexPath.row]
return cell
}
}
// MARK: - UserInputCellDelegate
extension ViewController: UserInputCellDelegate {
func didUpdate(cell: UserInputCell, string: String?) {
if let indexPath = tableView.indexPath(for: cell) {
datas[indexPath.row] = string ?? "" // update `datas` with value user edited
}
// For giggles and grins, let's print the array, so we can see what it's doing.
// In production app, this `print` statement would be removed.
print("\(datas)")
}
}
和
protocol UserInputCellDelegate: class { // this is class protocol, to allow weak reference
/// When text field is updated, cell calls this delegate method to inform it of changes
/// to text field value.
///
/// - Parameters:
/// - cell: Cell containing text field that was updated
/// - string: String value entered.
func didUpdate(cell: UserInputCell, string: String?)
}
class UserInputCell: UITableViewCell, UITextFieldDelegate {
weak var delegate: UserInputCellDelegate? // note this is weak to avoid strong reference cycle
@IBOutlet weak var usernameTextField: UITextField!
// hooked up to "Editing did end" action for text field in IB
@IBAction func didEndEditing(_ sender: UITextField) {
delegate?.didUpdate(cell: self, string: sender.text)
}
}
关于ios - Swift:在 TableView 中插入新的 CustomTableViewCell 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42707554/