Swift TableView Crash: “Missing cell for newly visible row”

好久不见. 提交于 2019-12-09 21:41:44

问题


Im experiencing a crash when adding new rows to a tableview. In short, the crash log says "Missing cell for newly visible row 3".

Reproduction
1. Add N amount of objects to datasource
2. Manually add the same amount of cells to tableview
3. Reload and animate using beginUpdates - endUpdates

Known Problem
This crash has already been discussed at question and reported at Apple. Their solution to this problem (not using estimated cell heights) does not work for me as i need 2 different heights for the cells.

My tableview is composed out of 2 different cell classes. Both have their own standard height, configured as such:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if indexPath.row % (straightsetLogs.count + 1) == 0 {
        return 44
    } else {
        return tableView.rowHeight
    }
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // (exerciseSets.count / straightsetLogs.count) = amount of (super)sets
    // (straightsetLogs.count + 1) = amount of cells needed for 1 (super)set
    return (exerciseSets.count / straightsetLogs.count) * (straightsetLogs.count + 1)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    // Headercell: 1 headercell for every (super)set
    if indexPath.row % (straightsetLogs.count + 1) == 0 {
        // Create header cells once for every set
        let headerCell = tableView.dequeueReusableCell(withIdentifier: CellID.setHeaderCell, for: indexPath) as! SetHeaderTableViewCell

        return configureHeaderCell(headerCell, forIndexPath: indexPath, totalCellsPerSet: straightsetLogs.count + 1)
    } else {
        // Create picker cells for rest of rows
        let pickerCell = tableView.dequeueReusableCell(withIdentifier: CellID.setPickerCell, for: indexPath) as! SetPickerTableViewCell

        // Configure according to set and movement
        return configurePickerCell(pickerCell, forIndexPath: indexPath, totalCellsPerSet: straightsetLogs.count + 1)
    }
}

In storyboard i've configured the tableview itself to have a row height of 260.0 and for the header cell i've checked the custom row height box and set it to 44.0.

Current Status
The code for determining the correct cells for the index paths works and the crash can be solved by removing the height for row at indexPath code. But then i end up with all cells being 260.0 height.

Goal
I need 44.0 height for header cells and 260.0 height for picker cells without experiencing this crash.

来源:https://stackoverflow.com/questions/41360840/swift-tableview-crash-missing-cell-for-newly-visible-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!