ios - swift 错误 : index out of range

标签 ios swift xcode uitableview swift3

我正在处理我的 tableView,刚才我实现了删除行的功能,但是在我删除一行之后,应用程序崩溃并出现此错误( fatal error :索引超出范围)

struct User {

        var name: String
        var images: UIImage
        var coordinate : (Double , Double)
        var type: String
        var address: String
    }


      var users = [User]()




override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
            rows = 0
            tableView.reloadData()
            insertRowsMode3()
    }

        @IBAction func refreshTapped(_ sender: Any) {

            rows = 0
            tableView.reloadData()
            insertRowsMode3()
        }

        func insertRowsMode2() {

            for i in 0..<users.count {
                insertRowMode2(ind: i, usr: users[i])
            }            
        }

        func insertRowMode2(ind:Int,usr:User) {

            let indPath = IndexPath(row: ind, section: 0)

            rows = ind + 1
          tableView.insertRows(at: [indPath], with: .right)
        }        

        func insertRowsMode3() {

            rows = 0            
            insertRowMode3(ind: 0)
        }

        func insertRowMode3(ind:Int) {

            let indPath = IndexPath(row: ind, section: 0)                        
            rows = ind + 1                       
            tableView.insertRows(at: [indPath], with: .right)                        

            guard ind < users.count-1 else { return }
            DispatchQueue.main.asyncAfter(deadline: .now()+0.20) {                
                self.insertRowMode3(ind: ind+1)
            }
        }

        func numberOfSections(in tableView: UITableView) -> Int {           
            return 1
        }        

        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {            
            return rows
        }

        public  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyTableViewCell

            let user = users[indexPath.row]

            cell.selectionStyle = .none

            cell.myImage.image = user.images
            cell.myLabel.text = user.name
            cell.myTypeLabel.text = user.type
            return (cell)
        }

         func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

            tableView.deselectRow(at: indexPath, animated: true)

            UIView.animate(withDuration: 0.2, animations: {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyTableViewCell
            })

            performSegue(withIdentifier: "goToLast" , sender: users[indexPath.row])

        }

         func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return 100
        }

        func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
            return true
        }

        func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

            if editingStyle == UITableViewCellEditingStyle.delete {

                users.remove(at: indexPath.row)
                tableView.reloadData()

            }           
        }

这是我的 tableView 的部分代码,我想我知道为什么应用程序会崩溃,但我不知道如何解决这个问题,我能做什么?

最佳答案

根据@vadian 的评论,更新您的numberOfRowsInSectionas 而不是return rows return return users.count。 和 用这个更新删除代码

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

关于ios - swift 错误 : index out of range,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46360998/

相关文章:

ios - 如何将数据从数组传递到测验应用程序的多个标签?

ios - 当对象在ios中执行方法时,是否有发布通知的解决方案

ios - 解析 Json 位置数据

iphone - 如何在 iOS 中创建邮政编码字段

swift - 我们可以在枚举上使用 `if` 或 `guard` 而不是 `switch` 来提取值吗?

objective-c - 如何知道用户何时单击 WebView 中的目标 ="_blank"链接?

iphone - 如何在 iOS 5 的 segue 之间设置 Storyboard UIViewController 的属性?

ios - 验证应用程序时出现 iTunes 商店操作失败错误

iOS Today 扩展无法构建

swift - 如何从函数中取出纬度和经度的值,以便它们可以在 VC 中的其他任何地方使用?