删除单元格给出错误和崩溃的应用程序

Posted

技术标签:

【中文标题】删除单元格给出错误和崩溃的应用程序【英文标题】:Deleting cell giving error and crashing app 【发布时间】:2020-08-06 14:44:37 【问题描述】:

我需要一些帮助来找出我收到此错误的原因。我目前有一个附加结构的视图控制器(我们称之为 ADDPersonVC)。该结构如下所示:

import UIKit

struct Person 
    var Name: String?
    var PostNumber: String?
    var StoryNumber: String?
    var Compensation: String?
    var ProfileImageUrl: String?

我在 ADDPersonVC 中有一个按钮,它在第二个视图控制器(我们称之为考虑因素TestVC)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 时也遇到了问题,但这是我弄清楚后的问题。

感谢所有帮助。

编辑:

当我在删除函数上运行断点并获取它返回的值时:

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

【问题讨论】:

删除前设置断点了吗?您是否在 .people 更改时重新加载 tableView?您可以在每一步检查您的静态数组,以确保其行为符合您的预期 我设置了断点,它显示的只是整个数组。我不知道如何在 indexPath.row 中附加数组/删除特定的人。我真的需要一些帮助。 编译器说索引超出范围,你检查索引了吗?当您确实更新了数据源(即 .people 数组)并忘记重新加载 tableView 时,可能会发生这种情况。你会在数组更改时调用 tableView.reloadData 吗? 当使用以下代码在 AddPersonVC 中点击 addPersonButton 时,我正在重新加载表格: DispatchQueue.main.async NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load" ), 对象: nil) 更新数据源后忘记更新 tableView。添加tableView.deleteRows(at: [indexPath], with: .fade) 【参考方案1】:

我需要在我的删除函数中添加以下代码"

tableView.deleteRows(at: [indexPath], with: .fade)

【讨论】:

以上是关于删除单元格给出错误和崩溃的应用程序的主要内容,如果未能解决你的问题,请参考以下文章

当我滑动删除时,表格单元格崩溃。获取错误的访问错误

滑动删除 UITableView 单元格动画在 swift 2 中不起作用

当我在按钮操作上从 TableView 中删除单元格时,应用程序崩溃了 - iOS Swift

在uitableview单元格上留下滑动删除按钮打开然后返回导致崩溃

从 UITableView 中删除值会导致崩溃

触摸单元格时 UICollectionView 崩溃