如何在 TableView 中执行 CRUD 操作 Swift
How to Perform CRUD operation in TableView Swift
我已经编写了执行删除、插入、更新的代码。当我执行我的代码时,我收到类似“由于未捕获的异常 'NSInternalInconsistencyException' 而终止应用程序”之类的错误,原因:'与 UITableView 一起使用的索引路径无效。传递给 table 视图的索引路径必须恰好包含两个指定部分和行的索引。如果可能,请在 NSIndexPath+UIKitAdditions.h 中使用 NSIndexPath 上的类别。”
我在这里添加代码
import UIKit
class ViewController: UIViewController {
@IBOutlet var tableView: UITableView!
@IBOutlet var Insert: UIButton!
@IBOutlet var txtfield: UITextField!
var index = IndexPath()
var models = ["1.Audi","2.Hyundai","3.Bentley","4.Chevrolet","5.Dodge","6.Electric","7.Ford","8.Genesis","9.Honda","10.Ferrari","11.Nissan","12.Porche","13.Range Rover","14.Lamborgini","15.McLaren","16.koneisegg","17.Volvo","18.Mazda","19.Bmw"]
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
@IBAction func textFieldEdit(_ sender: UITextField) {
//self.tableView.reloadData()
if let cell = tableView.cellForRow(at: index) as? UITableViewCell{
cell.textLabel?.text = self.txtfield.text
}
}
@IBAction func insertBtn(_ sender: UIButton) {
if let txt = txtfield.text, !txt.isEmpty{
//self.models.append(txt)
self.models.insert(txt,at: 0)
tableView.beginUpdates()
tableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .fade)
tableView.endUpdates()
print (models.count)
}
}
}
extension ViewController: UITableViewDataSource,UITableViewDelegate{
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return models.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = models[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
return .delete
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete{
tableView.beginUpdates()
models.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
tableView.endUpdates()
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
index = indexPath
self.txtfield.text = models[indexPath.row]
}
}
ScreenShot of My storyBoard
您实例化了一个 IndexPath
没有属性
var index = IndexPath()
如果您在设置有效 属性 之前参考它,您将收到该错误。
可能的解决方案
var index: IndexPath?
为您的索引使用可选值,而不是设置无效的默认值 属性。在您分配一个实例之前它将为零。
在使用 属性 时,您必须使用 index?.
或 if let index = index
,但它是安全的。
你没有解释 当你得到错误。
运行 您的代码原样,没有明显的错误。
但是,我的猜测是当您还没有选择行时,或者当您在文本字段中编辑时做了一些更改选择的操作时遇到了问题。
尝试将您的 textFieldEdit
函数更改为:
@IBAction func textFieldEdit(_ sender: UITextField) {
// make sure
// a row is selected
// and
// we can get a reference to that cell
guard let pth = tableView.indexPathForSelectedRow,
let cell = tableView.cellForRow(at: pth)
else {
return
}
// properly unwrap optional .text property
let str = sender.text ?? ""
// update the data
self.models[pth.row] = str
// update the cell
cell.textLabel?.text = str
}
我已经编写了执行删除、插入、更新的代码。当我执行我的代码时,我收到类似“由于未捕获的异常 'NSInternalInconsistencyException' 而终止应用程序”之类的错误,原因:'与 UITableView 一起使用的索引路径无效。传递给 table 视图的索引路径必须恰好包含两个指定部分和行的索引。如果可能,请在 NSIndexPath+UIKitAdditions.h 中使用 NSIndexPath 上的类别。” 我在这里添加代码
import UIKit
class ViewController: UIViewController {
@IBOutlet var tableView: UITableView!
@IBOutlet var Insert: UIButton!
@IBOutlet var txtfield: UITextField!
var index = IndexPath()
var models = ["1.Audi","2.Hyundai","3.Bentley","4.Chevrolet","5.Dodge","6.Electric","7.Ford","8.Genesis","9.Honda","10.Ferrari","11.Nissan","12.Porche","13.Range Rover","14.Lamborgini","15.McLaren","16.koneisegg","17.Volvo","18.Mazda","19.Bmw"]
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
@IBAction func textFieldEdit(_ sender: UITextField) {
//self.tableView.reloadData()
if let cell = tableView.cellForRow(at: index) as? UITableViewCell{
cell.textLabel?.text = self.txtfield.text
}
}
@IBAction func insertBtn(_ sender: UIButton) {
if let txt = txtfield.text, !txt.isEmpty{
//self.models.append(txt)
self.models.insert(txt,at: 0)
tableView.beginUpdates()
tableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .fade)
tableView.endUpdates()
print (models.count)
}
}
}
extension ViewController: UITableViewDataSource,UITableViewDelegate{
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return models.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = models[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
return .delete
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete{
tableView.beginUpdates()
models.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
tableView.endUpdates()
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
index = indexPath
self.txtfield.text = models[indexPath.row]
}
}
ScreenShot of My storyBoard
您实例化了一个 IndexPath
没有属性
var index = IndexPath()
如果您在设置有效 属性 之前参考它,您将收到该错误。
可能的解决方案
var index: IndexPath?
为您的索引使用可选值,而不是设置无效的默认值 属性。在您分配一个实例之前它将为零。
在使用 属性 时,您必须使用 index?.
或 if let index = index
,但它是安全的。
你没有解释 当你得到错误。
运行 您的代码原样,没有明显的错误。
但是,我的猜测是当您还没有选择行时,或者当您在文本字段中编辑时做了一些更改选择的操作时遇到了问题。
尝试将您的 textFieldEdit
函数更改为:
@IBAction func textFieldEdit(_ sender: UITextField) {
// make sure
// a row is selected
// and
// we can get a reference to that cell
guard let pth = tableView.indexPathForSelectedRow,
let cell = tableView.cellForRow(at: pth)
else {
return
}
// properly unwrap optional .text property
let str = sender.text ?? ""
// update the data
self.models[pth.row] = str
// update the cell
cell.textLabel?.text = str
}