简体   繁体   English

在表中插入一行时,“试图将第0行插入第0部分,但更新后第0部分只有0行”

[英]'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update' when inserting a row into a table

I've been getting this error when I try to insert a row in my table. 当我尝试在表中插入一行时,出现了此错误。 I've looked at the other answers for hours but either they are doing something different or it is written in Objective C. I'm very new to swift so I may be doing something obviously wrong that i just cant figure out. 我已经看了几个小时的其他答案,但是要么他们做的是不同的事情,要么是用目标C编写的。我是新手,所以我可能做的很明显是我无法弄清楚的错误。

Here is my code: 这是我的代码:

import UIKit
import Alamofire
import SwiftyJSON
import SwiftDate

class LaunchesViewController: UIViewController {

    @IBOutlet var launchesTable: UITableView!

    var launches : [Launch]!

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view.

        loadLaunches()
    }

    func loadLaunches()
    {

        Alamofire.request("http://10.0.0.26:5000/V1/Launches/Test", method: .get).validate().responseJSON { response in

            let json = JSON(response.result.value!)

            let jsonArray = json.arrayValue

            self.launches = []

            for jsonObject in jsonArray {

                self.launches.append(Launch(launchDate:     jsonObject["LaunchDate"].string?.toDate(),  launchName: jsonObject["LaunchName"].string))
            }

            self.onLaunchesLoaded()
        }
    }

    func onLaunchesLoaded()
    {
        self.launchesTable.beginUpdates()
        self.launchesTable.insertRows(at: [IndexPath.init(row: 0, section: 0)], with: .none)
        self.launchesTable.endUpdates()
    }

}

You should configure your table view's data source properly. 您应该正确配置表格视图的数据源。 First, specify the number of sections via numberOfSectionsInTableView: and number of rows tableView:numberOfRowsInSection: based on the data that is returned by your network request. 首先,根据网络请求返回的数据,通过numberOfSectionsInTableView:指定节数,并通过tableView:numberOfRowsInSection:指定行数。 Also, because table view cells are reused the best way to populate the table view cells is through the table view data source tableView:cellForRowAtIndexPath: method instead of calling insertRows() method. 另外,由于表视图单元格已被重用,因此填充表视图单元格的最佳方法是通过表视图数据源tableView:cellForRowAtIndexPath:方法,而不是调用insertRows()方法。 This should be covered in the Basics of TableView Creation . 这应该在TableView Creation基础知识中进行介绍。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “尝试将第0行插入第0部分,但更新后第0部分只有0行” - 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update' 尝试将第0行插入第0部分,但更新后第0部分只有0行 - attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update Swift-“尝试将第0行插入第0节,但更新后第0节只有0行” - Swift - “attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update” “尝试将第0行插入第0部分,但更新后第0部分只有0行”错误 - “Attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update” Error 尝试将第 3 行插入第 0 节,但更新后第 0 节中只有 3 行 - attempt to insert row 3 into section 0, but there are only 3 rows in section 0 after the update 尝试插入行时“尝试将第 0 行插入第 0 节,但更新后第 0 节中只有 0 行” - "attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update" when trying to insert row 'NSInternalInconsistencyException',原因:“试图将第0行插入第0节,但更新后第0节只有0行” - 'NSInternalInconsistencyException', reason: 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update' 'NSInternalInconsistencyException',原因:“试图将第4行插入第0节,但更新后第0节只有4行” - 'NSInternalInconsistencyException', reason: 'attempt to insert row 4 into section 0, but there are only 4 rows in section 0 after the update' SwiftUI 应用程序因“尝试将第 2 行插入第 0 部分,但更新后第 0 部分中只有 2 行”错误而崩溃 - SwiftUI app crash with “attempt to insert row 2 into section 0, but there are only 2 rows in section 0 after the update” error NSInternalInconsistencyException',原因:'尝试将第0行插入第0部分,但更新后第0部分只有0行' - NSInternalInconsistencyException', reason: 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM