我已經撰寫了用于執行 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]
}
}
我的故事板的螢屏截圖
uj5u.com熱心網友回復:
你已經實體化了一個IndexPath沒有屬性的
var index = IndexPath()
如果您在設定有效屬性之前參考它,則會收到該錯誤。
可能的解決方案
var index: IndexPath?
為您的索引使用可選項,而不是設定無效的默認屬性。在您分配實體之前,它將為零。
然后您將不得不使用index?.或if let index = index使用該屬性時,但它會是安全的。
uj5u.com熱心網友回復:
當您收到錯誤時,您沒有解釋。
按原樣運行您的代碼,沒有明顯錯誤。
但是,我的猜測是,當您還沒有選擇一行時,或者當您在文本欄位中編輯時做了一些更改選擇的操作時,您會遇到問題。
嘗試將您的textFieldEditfunc更改為:
@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
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/359220.html
上一篇:在UIKit中顯示領域資料
