UITableViewCell - 在动态表视图中获取 UITextField 值
UITableViewCell - Get UITextField values in dynamic tableview
问题是,我已经找到的大多数解决方案都涉及使用 self.myTableView.cellForRow(at: index) as!
方法。
问题是,如果你有一个大的 UITableView,单元格最终会被重用,你就会失去引用。
另一种方法涉及使用 textFieldDidEndEditing(_ textField: UITextField)
,但我不知道这是否是最优雅的解决方案。
这是我正在使用的一些代码:
protocol TableViewCompatible {
var reuseIdentifier: String { get }
func cellForTableView(tableView: UITableView, atIndexPath indexPath: IndexPath) -> UITableViewCell
}
class NameCellDTO: TableViewCompatible {
var reuseIdentifier: String {
return "NameCell"
}
var name: String?
var placeHolder: String
init(name: String? = nil, placeHolder: String) {
self.name = name
self.placeHolder = placeHolder
}
func cellForTableView(tableView: UITableView, atIndexPath indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: self.reuseIdentifier, for: indexPath) as! NameCell
cell.configureWithDTO(self)
return cell
}
}
DTO 创建:
let nameCell = NameCellDTO(name: "Test", placeHolder: "attributes.user.first_name".localized())
我有一个包含一些 TableViewCompatible
DTO 的数组
和 cellForRow :
func cellForRowAt(tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let item = items.object(index: indexPath.row) else {
return UITableViewCell()
}
return item.cellForTableView(tableView: tableView, atIndexPath: indexPath)
}
你可以试试
var arr = [String](repeating: "", count: countOfRows)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = ///
cell.myTextF.delegate = self
cell.myTextF.tag = indexPath.row
cell.myTextF.text = arr[indexPath.row]
}
func textFieldDidEndEditing(_ textField: UITextField) {
arr[textField.tag] = textField.text!
}
问题是,我已经找到的大多数解决方案都涉及使用 self.myTableView.cellForRow(at: index) as!
方法。
问题是,如果你有一个大的 UITableView,单元格最终会被重用,你就会失去引用。
另一种方法涉及使用 textFieldDidEndEditing(_ textField: UITextField)
,但我不知道这是否是最优雅的解决方案。
这是我正在使用的一些代码:
protocol TableViewCompatible {
var reuseIdentifier: String { get }
func cellForTableView(tableView: UITableView, atIndexPath indexPath: IndexPath) -> UITableViewCell
}
class NameCellDTO: TableViewCompatible {
var reuseIdentifier: String {
return "NameCell"
}
var name: String?
var placeHolder: String
init(name: String? = nil, placeHolder: String) {
self.name = name
self.placeHolder = placeHolder
}
func cellForTableView(tableView: UITableView, atIndexPath indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: self.reuseIdentifier, for: indexPath) as! NameCell
cell.configureWithDTO(self)
return cell
}
}
DTO 创建:
let nameCell = NameCellDTO(name: "Test", placeHolder: "attributes.user.first_name".localized())
我有一个包含一些 TableViewCompatible
DTO 的数组
和 cellForRow :
func cellForRowAt(tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let item = items.object(index: indexPath.row) else {
return UITableViewCell()
}
return item.cellForTableView(tableView: tableView, atIndexPath: indexPath)
}
你可以试试
var arr = [String](repeating: "", count: countOfRows)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = ///
cell.myTextF.delegate = self
cell.myTextF.tag = indexPath.row
cell.myTextF.text = arr[indexPath.row]
}
func textFieldDidEndEditing(_ textField: UITextField) {
arr[textField.tag] = textField.text!
}