У меня есть следующие четыре списка продуктов:
var products1 = [Product]()
var products2 = [Product]()
var products3 = [Product]()
var products4 = [Product]()
Я хотел бы иметь tableView с несколькими разделами, включая заголовки. У меня нет проблем с добавлением заголовков без ошибок; но когда дело доходит до числа, то это дает мне ошибку.
это мой numberOfSections
:
func numberOfSections(in tableView: UITableView) -> Int {
var numberOfSections:Int = 1
if orderDetails.status == DatabaseRef.Order_Received {
numberOfSections = 1
}else
if orderDetails.status == DatabaseRef.Picking {
numberOfSections = 2
} else
if orderDetails.status == DatabaseRef.ReadyForDelivery || orderDetails.status == DatabaseRef.OnTheWay || orderDetails.status == DatabaseRef.Delivered {
numberOfSections = 4
}
return numberOfSections
}
Основано на статусе заказа; Я хочу иметь разное количество разделов (1 раздел, 2 раздела или 4 раздела). Но я продолжаю получать ошибку, что это вне диапазона. Вот как я кодировал число строк:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var numberOfRows = 0
if orderDetails.status == DatabaseRef.Order_Received {
numberOfRows = products1.count
}else
if orderDetails.status == DatabaseRef.Picking {
if section == 0 {
numberOfRows = products1.count
} else
if section == 1 {
numberOfRows = products2.count
}
} else
if orderDetails.status == DatabaseRef.ReadyForDelivery || orderDetails.status == DatabaseRef.OnTheWay || orderDetails.status == DatabaseRef.Delivered {
if section == 0 {
numberOfRows = products1.count
} else
if section == 1 {
numberOfRows = products2.count
} else
if section == 2 {
numberOfRows = products3.count
} else
if section == 3 {
numberOfRows = products4.count
}
}
return numberOfRows
}
Это мое cellForRoaAt
:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: Identifiers.MyOrderCartCell, for: indexPath) as? MyOrderCartCell {
if orderDetails.status == DatabaseRef.Order_Received {
if indexPath.section == 0 {
cell.configureCell(order: orderDetails, product: products1[indexPath.row], delegate: self)
}
} else
if orderDetails.status == DatabaseRef.Picking {
if indexPath.section == 0 {
cell.configureCell(order: orderDetails, product: products1[indexPath.row], delegate: self)
} else
if indexPath.section == 1 {
cell.configureCell(order: orderDetails, product: products2[indexPath.row], delegate: self)
}
} else
if orderDetails.status == DatabaseRef.ReadyForDelivery || orderDetails.status == DatabaseRef.OnTheWay || orderDetails.status == DatabaseRef.Delivered {
if indexPath.section == 0 {
cell.configureCell(order: orderDetails, product: products1[indexPath.row], delegate: self)
} else
if indexPath.section == 1 {
cell.configureCell(order: orderDetails, product: products2[indexPath.row], delegate: self)
} else
if indexPath.section == 2 {
cell.configureCell(order: orderDetails, product: products3[indexPath.row], delegate: self)
} else
if indexPath.section == 3 {
cell.configureCell(order: orderDetails, product: products4[indexPath.row], delegate: self)
}
}
Что я здесь не так делаю? Сообщение об ошибке:
Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).