Swift TableView Crash: “Missing cell for newly vis

2019-04-14 06:24发布

问题:

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.