Noob here.
I am programmatically creating a number (globalCount
) of UISliders in iOS with Swift 3. A grandTotal
float value is divided between these sliders' values and when one is changed they all adjust to sum to 100% of the grandTotal
. No slider goes below their minimum of 0 or above their maximum of 100.
All is working well for numbers of sliders less than 7. As soon as I add 7 or more. I get a crash when trying to update the cell's slider values with the underlying struct values.
I've moved the cell update functionality to its own function to try isolate the issue. I still get a crash. The crash is
EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0)
and appears on the let loopCell
line.
Does anyone have any suggestions as to what this is about? Why is it working for low numbers and not higher numbers of cells? Is this a conditionals thing?
This is the cell update function
func updateCells() {
for i in 0...globalCount - 1 {
let loopCell: MyTableCell = ((myTableView.cellForRow(at: IndexPath(row:i, section: 0))) as? MyTableCell)!
loopCell.valueLabel.text = String(structData[i].value)
loopCell.cellSlider.value = structData[i].value
}
}
My UI:
cellForRow(at:)
returns nil
if a table view cell is not visible (currently offscreen). You are crashing because you are force unwrapping nil
. Instead, use optional binding (if let
) to safely unwrap the value:
func updateCells() {
for i in 0..<globalCount {
if let loopCell = myTableView.cellForRow(at: IndexPath(row: i, section: 0)) as? MyTableCell {
loopCell.valueLabel.text = String(structData[i].value)
loopCell.cellSlider.value = structData[i].value
}
}
}