Ios 是否在UITableViewCell中进行内联编辑?
我正在尝试构建一个使用UITableView和UITableView单元格的应用程序 我希望用户点击Add按钮将项目添加到TableView,而无需调用新窗口或警报弹出窗口。我还希望用户点击单元格编辑其值,然后保存它 我正在努力找到最好的方法。根据苹果公司关于UITableViewCell的文档,这似乎是不可能的Ios 是否在UITableViewCell中进行内联编辑?,ios,swift,Ios,Swift,我正在尝试构建一个使用UITableView和UITableView单元格的应用程序 我希望用户点击Add按钮将项目添加到TableView,而无需调用新窗口或警报弹出窗口。我还希望用户点击单元格编辑其值,然后保存它 我正在努力找到最好的方法。根据苹果公司关于UITableViewCell的文档,这似乎是不可能的 有更好的方法吗?这里是一个非常基本的例子 在情节提要中,添加嵌入到UINavigationController中的UITableViewController。将表视图控制器的自定义类设
有更好的方法吗?这里是一个非常基本的例子 在情节提要中,添加嵌入到
UINavigationController
中的UITableViewController
。将表视图控制器的自定义类设置为SampleTableViewController
。这就是运行此程序所需的全部操作
桌子一开始是空的。点击导航栏上的Add(“+”)按钮,将新项添加到数据数组并重新加载表
编辑文本字段时,文本将通过“回调”闭包传递回控制器,在这里我们用新字符串更新数据数组
还有一个“完成”按钮,点击它将简单地将数据数组打印到调试控制台,以便我们可以看到更改。这就是你要做的事情,比如保存用户输入的数据(或者你计划用它做的任何事情)
SampleTableViewController类
class SampleTableViewController: UITableViewController {
var myData: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
// cells will have text fields, so we want to be able to
// dismiss the keyboard by scrolling the table
tableView.keyboardDismissMode = .onDrag
// register our custom cell
tableView.register(SampleTextFieldCell.self, forCellReuseIdentifier: "cell")
// put system Add "+" button and system "Done" button
// on right side of the navigation bar
let addBtn = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(self.addButtonTapped))
let doneBtn = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(self.doneButtonTapped))
navigationItem.rightBarButtonItems = [doneBtn, addBtn]
}
@objc func addButtonTapped() -> Void {
// add a new element to data array
myData.append("")
// reload the table
tableView.reloadData()
}
@objc func doneButtonTapped() -> Void {
// do something with the added / edited items
// maybe save then to a database?
// for now, just print the data array to the debug console
print(myData)
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myData.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! SampleTextFieldCell
cell.theTextField.text = myData[indexPath.row]
// set the "callback" closure so we can save the text as its being edited
cell.callback = { str in
// update data array when text in cell is edited
self.myData[indexPath.row] = str
}
return cell
}
}
class SampleTextFieldCell: UITableViewCell {
let theTextField = UITextField()
// closure used to tell the controller that the text field has been edited
var callback: ((String) ->())?
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
func commonInit() -> Void {
theTextField.borderStyle = .roundedRect
theTextField.placeholder = "Enter new item..."
theTextField.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(theTextField)
let g = contentView.layoutMarginsGuide
NSLayoutConstraint.activate([
theTextField.topAnchor.constraint(equalTo: g.topAnchor, constant: 0.0),
theTextField.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 0.0),
theTextField.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: 0.0),
theTextField.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: 0.0),
])
theTextField.addTarget(self, action: #selector(self.textFieldEdited(_:)), for: .editingChanged)
}
@objc func textFieldEdited(_ textField: UITextField) -> Void {
// send newly edited text back to the controller
callback?(textField.text ?? "")
}
}
SampleTextFieldCell类
class SampleTableViewController: UITableViewController {
var myData: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
// cells will have text fields, so we want to be able to
// dismiss the keyboard by scrolling the table
tableView.keyboardDismissMode = .onDrag
// register our custom cell
tableView.register(SampleTextFieldCell.self, forCellReuseIdentifier: "cell")
// put system Add "+" button and system "Done" button
// on right side of the navigation bar
let addBtn = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(self.addButtonTapped))
let doneBtn = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(self.doneButtonTapped))
navigationItem.rightBarButtonItems = [doneBtn, addBtn]
}
@objc func addButtonTapped() -> Void {
// add a new element to data array
myData.append("")
// reload the table
tableView.reloadData()
}
@objc func doneButtonTapped() -> Void {
// do something with the added / edited items
// maybe save then to a database?
// for now, just print the data array to the debug console
print(myData)
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myData.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! SampleTextFieldCell
cell.theTextField.text = myData[indexPath.row]
// set the "callback" closure so we can save the text as its being edited
cell.callback = { str in
// update data array when text in cell is edited
self.myData[indexPath.row] = str
}
return cell
}
}
class SampleTextFieldCell: UITableViewCell {
let theTextField = UITextField()
// closure used to tell the controller that the text field has been edited
var callback: ((String) ->())?
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
func commonInit() -> Void {
theTextField.borderStyle = .roundedRect
theTextField.placeholder = "Enter new item..."
theTextField.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(theTextField)
let g = contentView.layoutMarginsGuide
NSLayoutConstraint.activate([
theTextField.topAnchor.constraint(equalTo: g.topAnchor, constant: 0.0),
theTextField.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 0.0),
theTextField.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: 0.0),
theTextField.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: 0.0),
])
theTextField.addTarget(self, action: #selector(self.textFieldEdited(_:)), for: .editingChanged)
}
@objc func textFieldEdited(_ textField: UITextField) -> Void {
// send newly edited text back to the controller
callback?(textField.text ?? "")
}
}
你在问如何做一大堆不同的事情。到目前为止你做了什么?可以从字符串数组中显示表格吗?你能创建一个连接到动作函数的“添加”按钮吗?你能创建一个带有文本字段的自定义单元格吗?现在,我有一个桌面视图,上面有一个编辑和添加按钮。单击“添加”后,将弹出一个警告框,要求我输入项目。我还没有编写任何逻辑代码来显示我添加到TableView中的项目,因为我不想坚持这种设计。@Caps6160-如果这回答了您的问题,请确保将其标记为已接受,以方便其他可能遇到它的人。