【问题标题】:How to Perform CRUD operation in TableView Swift如何在 TableView Swift 中执行 CRUD 操作
【发布时间】:2021-12-28 09:48:25
【问题描述】:

我已经编写了执行 Delete,Insert,Update 的代码。当我执行我的代码时,我收到一个错误,例如“由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'与 UITableView 一起使用的索引路径无效。传递给表视图的索引路径必须包含两个索引,指定节和行. 如果可能,请使用 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

【问题讨论】:

    标签: ios swift xcode uitableview


    【解决方案1】:

    你没有解释什么时候你收到了错误。

    按原样运行您的代码,没有明显的错误。

    但是,我的猜测是,当您还没有选择一行时,或者当您在文本字段中编辑时更改了选择时,您遇到了问题。

    尝试将您的 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
    }
    

    【讨论】:

    • 实际上我的错误是当我开始输入 textField 时,出现上述错误。在我刷新我的 tabledata 之后,更新的值就会改变
    • 现在它工作正常,但是当我输入一些内容并按下更新按钮时 我收到类似线程 1 的错误:“与 UITableView 一起使用的索引路径无效。传递给表视图的索引路径必须包含两个索引,指定节和行。如果可能,请在 NSIndexPath+UIKitAdditions.h 中使用 NSIndexPath 上的类别。"
    • @JestinSaji - 您没有为“更新”按钮添加任何代码。您希望您的数据随着用户输入而更新吗?或者,您是否只想在用户点击“更新”之后更新数据(和表格视图单元格)?
    【解决方案2】:

    你已经实例化了一个没有属性的IndexPath

    var index = IndexPath()
    

    如果您在设置有效属性之前引用它,您将收到该错误。

    可能的解决方案

    var index: IndexPath?
    

    为您的索引使用可选项,而不是设置无效的默认属性。在您分配实例之前,它将为零。

    然后您必须在使用该属性时使用index?.if let index = index,但这会很安全。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-23
      • 1970-01-01
      • 2019-06-24
      • 1970-01-01
      • 1970-01-01
      • 2016-07-01
      • 1970-01-01
      相关资源
      最近更新 更多