删除给出错误和崩溃应用程序的单元格
Deleting cell giving error and crashing app
我需要一些帮助来弄清楚为什么会出现此错误。我目前有一个附加结构的视图控制器(我们称之为 ADDPersonVC)。该结构如下所示:
import UIKit
struct Person {
var Name: String?
var PostNumber: String?
var StoryNumber: String?
var Compensation: String?
var ProfileImageUrl: String?
}
我在 ADDPersonVC 中有一个按钮,用于在第二个视图控制器(我们称之为 considerationsTestVC)的 tableViewCell 中附加标签。行数由:
决定
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return ConsiderationsTestViewController.people.count
}
附加结构的 addPersonButton 代码如下所示:
@IBAction func addButtonTapped(_ sender: Any) {
ConsiderationsTestViewController.people.append(Person(Name: "\(nameTextField.text!)", PostNumber: "\(gridTextField.text!)", StoryNumber: "\(storyTextField.text!)", Compensation: "\(compensationTextField.text!)", ProfileImageUrl: "\(userProfileImageUrlLabel.text!)"))
self.navigationController?.popViewController(animated: true)
DispatchQueue.main.async {
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil)
}
}
在 ConsiderationsTestViewController 中,我有一个名为 people 的静态变量,如下所示:
static var people: [Person] = []
然后我可以使用以下代码在 ConsiderationsTestViewContoller 中附加标签:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: AddPersonCell, for: indexPath) as! ConsiderationsCell
let numberOfPeopleCells = ConsiderationsTestViewController.people[indexPath.row]
cell.nameLabelC.text = numberOfPeopleCells.Name
cell.feedLabelC.text = numberOfPeopleCells.PostNumber
cell.storyLabelC.text = numberOfPeopleCells.StoryNumber
cell.compensationLabelC.text = numberOfPeopleCells.Compensation
cell.userImage.loadImageUsingCacheWithUrlString(urlString: numberOfPeopleCells.ProfileImageUrl!)
cell.userImage.layer.cornerRadius = 25
cell.nameLabelC.numberOfLines = 0
return cell
}
当我去删除单元格时,我使用的代码是:
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if (editingStyle == .delete) {
ConsiderationsTestViewController.people.remove(at: indexPath.row)
// handle delete (by removing the data from your array and updating the tableview)
}
}
当我在单元格上滑动并单击删除时出现错误:
"Thread 1: Fatal error: Index out of range"
行:
ConsiderationsTestViewController.people.remove(at: indexPath.row)
我整个星期都在研究这个问题,希望能得到一些帮助来解决这个问题。我很高兴我能够让结构充当数组并正确显示正确的信息。我在将它上传到 Firebase - realtimeDatabase 时也遇到了问题,但这是我弄清楚之后的问题。
感谢所有帮助。
编辑:
当我 运行 在删除函数上设置断点并获取值时 returns:
3 elements
▿ 0 : Person
▿ Name : Optional<String>
- some : "Person's Name1"
▿ PostNumber : Optional<String>
- some : "1"
▿ StoryNumber : Optional<String>
- some : "1"
▿ Compensation : Optional<String>
- some : "1"
▿ ProfileImageUrl : Optional<String>
- some : "PROFILEIMAGE URL HERE"
▿ userID : Optional<String>
- some : "B2Z4DlZ8RucvEQhz2NSUkquqc5P2"
▿ 1 : Person
▿ Name : Optional<String>
- some : "Person's Name2"
▿ PostNumber : Optional<String>
- some : "1"
▿ StoryNumber : Optional<String>
- some : "11"
▿ Compensation : Optional<String>
- some : "1"
▿ ProfileImageUrl : Optional<String>
- some : "PROFILEIMAGE URL HERE"
▿ userID : Optional<String>
- some : "Lbn9HL1WIpZTMFzpGWAXy7Ra0EA2"
▿ 2 : Person
▿ Name : Optional<String>
- some : "Person's Name3"
▿ PostNumber : Optional<String>
- some : "1"
▿ StoryNumber : Optional<String>
- some : "1"
▿ Compensation : Optional<String>
- some : "1"
▿ ProfileImageUrl : Optional<String>
- some : "PROFILEIMAGE URL HERE"
▿ userID : Optional<String>
- some : "NE6WVUfF6WQjJT9eeVSJgfvrrZW2"
我也试过这个:
po ConsiderationsTestViewController.people[indexPath.row]
我收到了以下信息:
Fatal error: Index out of range: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-1103.2.25.13/swift/stdlib/public/core/ContiguousArrayBuffer.swift, line 444
2020-08-06 11:14:34.945249-0700 BWM[10260:384634] Fatal error: Index out of range: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-1103.2.25.13/swift/stdlib/public/core/ContiguousArrayBuffer.swift, line 444
我需要在删除函数中添加以下代码
tableView.deleteRows(at: [indexPath], with: .fade)
我需要一些帮助来弄清楚为什么会出现此错误。我目前有一个附加结构的视图控制器(我们称之为 ADDPersonVC)。该结构如下所示:
import UIKit
struct Person {
var Name: String?
var PostNumber: String?
var StoryNumber: String?
var Compensation: String?
var ProfileImageUrl: String?
}
我在 ADDPersonVC 中有一个按钮,用于在第二个视图控制器(我们称之为 considerationsTestVC)的 tableViewCell 中附加标签。行数由:
决定func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return ConsiderationsTestViewController.people.count
}
附加结构的 addPersonButton 代码如下所示:
@IBAction func addButtonTapped(_ sender: Any) {
ConsiderationsTestViewController.people.append(Person(Name: "\(nameTextField.text!)", PostNumber: "\(gridTextField.text!)", StoryNumber: "\(storyTextField.text!)", Compensation: "\(compensationTextField.text!)", ProfileImageUrl: "\(userProfileImageUrlLabel.text!)"))
self.navigationController?.popViewController(animated: true)
DispatchQueue.main.async {
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil)
}
}
在 ConsiderationsTestViewController 中,我有一个名为 people 的静态变量,如下所示:
static var people: [Person] = []
然后我可以使用以下代码在 ConsiderationsTestViewContoller 中附加标签:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: AddPersonCell, for: indexPath) as! ConsiderationsCell
let numberOfPeopleCells = ConsiderationsTestViewController.people[indexPath.row]
cell.nameLabelC.text = numberOfPeopleCells.Name
cell.feedLabelC.text = numberOfPeopleCells.PostNumber
cell.storyLabelC.text = numberOfPeopleCells.StoryNumber
cell.compensationLabelC.text = numberOfPeopleCells.Compensation
cell.userImage.loadImageUsingCacheWithUrlString(urlString: numberOfPeopleCells.ProfileImageUrl!)
cell.userImage.layer.cornerRadius = 25
cell.nameLabelC.numberOfLines = 0
return cell
}
当我去删除单元格时,我使用的代码是:
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if (editingStyle == .delete) {
ConsiderationsTestViewController.people.remove(at: indexPath.row)
// handle delete (by removing the data from your array and updating the tableview)
}
}
当我在单元格上滑动并单击删除时出现错误:
"Thread 1: Fatal error: Index out of range"
行:
ConsiderationsTestViewController.people.remove(at: indexPath.row)
我整个星期都在研究这个问题,希望能得到一些帮助来解决这个问题。我很高兴我能够让结构充当数组并正确显示正确的信息。我在将它上传到 Firebase - realtimeDatabase 时也遇到了问题,但这是我弄清楚之后的问题。
感谢所有帮助。
编辑:
当我 运行 在删除函数上设置断点并获取值时 returns:
3 elements
▿ 0 : Person
▿ Name : Optional<String>
- some : "Person's Name1"
▿ PostNumber : Optional<String>
- some : "1"
▿ StoryNumber : Optional<String>
- some : "1"
▿ Compensation : Optional<String>
- some : "1"
▿ ProfileImageUrl : Optional<String>
- some : "PROFILEIMAGE URL HERE"
▿ userID : Optional<String>
- some : "B2Z4DlZ8RucvEQhz2NSUkquqc5P2"
▿ 1 : Person
▿ Name : Optional<String>
- some : "Person's Name2"
▿ PostNumber : Optional<String>
- some : "1"
▿ StoryNumber : Optional<String>
- some : "11"
▿ Compensation : Optional<String>
- some : "1"
▿ ProfileImageUrl : Optional<String>
- some : "PROFILEIMAGE URL HERE"
▿ userID : Optional<String>
- some : "Lbn9HL1WIpZTMFzpGWAXy7Ra0EA2"
▿ 2 : Person
▿ Name : Optional<String>
- some : "Person's Name3"
▿ PostNumber : Optional<String>
- some : "1"
▿ StoryNumber : Optional<String>
- some : "1"
▿ Compensation : Optional<String>
- some : "1"
▿ ProfileImageUrl : Optional<String>
- some : "PROFILEIMAGE URL HERE"
▿ userID : Optional<String>
- some : "NE6WVUfF6WQjJT9eeVSJgfvrrZW2"
我也试过这个:
po ConsiderationsTestViewController.people[indexPath.row]
我收到了以下信息:
Fatal error: Index out of range: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-1103.2.25.13/swift/stdlib/public/core/ContiguousArrayBuffer.swift, line 444
2020-08-06 11:14:34.945249-0700 BWM[10260:384634] Fatal error: Index out of range: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-1103.2.25.13/swift/stdlib/public/core/ContiguousArrayBuffer.swift, line 444
我需要在删除函数中添加以下代码
tableView.deleteRows(at: [indexPath], with: .fade)