Swift-在手动输入数据后编辑表格视图
我正在尝试创建一个选中/取消选中项目的任务列表 我有一个表视图,我希望用户能够在其中输入项目,并在需要新项目时扩展表 我可以在列表中输入数据,当我输入数据时,表格会增长 但是,如果我需要在列表中重新键入一个条目(即更正拼写错误),当我选择要更正的单元格时,我会在表中得到一个新单元格,其中我不需要一个单元格,并且我输入的更正文本会附加到数组中,而不是在列表中的列表位置输入文本 请帮帮我 这是我的密码: ListTableViewControlerSwift-在手动输入数据后编辑表格视图,swift,uitableview,didselectrowatindexpath,Swift,Uitableview,Didselectrowatindexpath,我正在尝试创建一个选中/取消选中项目的任务列表 我有一个表视图,我希望用户能够在其中输入项目,并在需要新项目时扩展表 我可以在列表中输入数据,当我输入数据时,表格会增长 但是,如果我需要在列表中重新键入一个条目(即更正拼写错误),当我选择要更正的单元格时,我会在表中得到一个新单元格,其中我不需要一个单元格,并且我输入的更正文本会附加到数组中,而不是在列表中的列表位置输入文本 请帮帮我 这是我的密码: ListTableViewControler import UIKit import os.lo
import UIKit
import os.log
class ListTableViewController: UITableViewController {
//MARK: Properties
var listText: [String] = [""]
var ip: IndexPath = []
var currentRow = Int()
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
self.navigationItem.leftBarButtonItem = self.editButtonItem
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return listText.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Configure the cell...
let cellIdentifier = "ListTableViewCell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! ListTableViewCell
return cell
}
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
// test to see if the index is empty if it is empty then disable the delete button.
if editingStyle == .delete {
// Delete the row from the data source
listText.remove(at: currentRow)
tableView.deleteRows(at: [IndexPath(row: currentRow, section: 0)], with: .automatic)
tableView.reloadData()
} else if editingStyle == .insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}else {
// disable Delete button if this is the last cell
self.editButtonItem.isEnabled = false
}
}
//MARK: Action
@IBAction func update_Table(_ sender: UITextField) {
let appendIndexPath = IndexPath(row: listText.endIndex, section: 0)
//update table
tableView.beginUpdates()
listText[listText.count - 1] = sender.text!
listText.append("")
tableView.insertRows(at: [appendIndexPath], with: .automatic)
tableView.endUpdates()
}
}
ListViewCell
class ListTableViewCell: UITableViewCell, UITextFieldDelegate {
@IBOutlet weak var checkMark: UIButton!
@IBOutlet weak var itemText: UITextField!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
itemText.delegate = self
checkMark.isSelected = false
itemText.becomeFirstResponder()
}
//Mark: UITextfieldDelegate
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
//Hide Keyboard
itemText.resignFirstResponder()
return true
}
// reads the texts field data
func textFieldDidEndEditing(_ textField: UITextField) {
itemText.text = textField.text
}
//Mark: Action
@IBAction func itemChecked(_ sender: UIButton) {
// define button appearance for each state
checkMark.setImage(UIImage.checkmark, for: UIControl.State.selected)
let attributedString = NSMutableAttributedString(string: itemText.text!)
//check button doesn't response if the text field is empty
if itemText.text?.isEmpty == false {
// Change the state of the button and change the appearance of the check box
if checkMark.isSelected == false {
checkMark.isSelected = true
attributedString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSMakeRange(0, attributedString.length))
itemText.attributedText = attributedString
}else{
checkMark.isSelected = false
attributedString.removeAttribute(NSAttributedString.Key.strikethroughStyle, range: NSMakeRange(0, attributedString.length))
itemText.attributedText = attributedString
}
}
}
}
你的方法是错误的。您必须在控制器中声明一个数据模型,并使用回调或协议/委托从单元接收更改并更新数据模型。即使选择了
,
状态也应该在模型中保持。感谢您指出了正确的方向。