У меня возникают некоторые проблемы с добавлением новых ячеек в табличном представлении.
Странно то, что я запускаю, как только функция работает без проблем, если я делаю это во второй раз, то это происходит sh с этой ошибкой.
* Завершение приложения из-за необработанного исключения 'NSRangeException', причина: '* - [__ NSSingleObjectArrayI objectAtIndex:]: индекс 1 за пределами [0 .. 0 ] '
Вот мой код:
override func viewWillAppear(_ animated: Bool) {
if prodottoDaAggiungere != nil {
prodotti.append(prodottoDaAggiungere!)
let indexPath = IndexPath(row: prodotti.count-1, section: 1)
tableView.insertRows(at: [indexPath], with: .fade) // ?
prodottoDaAggiungere = nil
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row < prodotti.count && indexPath.section == 1 {
let cell = tableView.dequeueReusableCell(withIdentifier: "ProdottoTableViewCell", for: indexPath) as! ProdottoTableViewCell // Crash :|
let indexRow = indexPath.row // Fix
cell.title.text = "\(prodotti[indexRow].prodotto.nome!) - \(prodotti[indexRow].prodotto.marca!)"
cell.subtitle.text = "\(prodotti[indexRow].prodotto.formati[prodotti[indexRow].formato].dimensione) \(prodotti[indexRow].prodotto.unitàMisura!) - \(prodotti[indexRow].prodotto.formati[prodotti[indexRow].formato].prezzo) €"
cell.number.text = Int(cell.stepper.value).description // 1
cell.stepper.addTarget(self, action: #selector(stepperChanged), for: .valueChanged)
return cell
}
return super.tableView(tableView, cellForRowAt: indexPath)
}
Используя точку останова, я создал приложение cra sh на dequeueReusableCell, но я не могу понять почему, кто-то может сказать мне, почему этот код sh?
Вот мой столикПросмотреть номерOfRowsInSection fun c:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
if section == 0 {
return 1
} else if section == 1 {
return 1+prodotti.count
} else {
return 0
}
}
![Storyboard](https://i.stack.imgur.com/53QEH.png)
![Cell](https://i.stack.imgur.com/Huc3Q.png)
![Result](https://i.stack.imgur.com/xue99.jpg)
Действительно спасибо AP