Tableview Multi-Sections 行数错误
Tableview Multi-Sections Number of Rows Error
我有以下四个产品列表:
var products1 = [Product]()
var products2 = [Product]()
var products3 = [Product]()
var products4 = [Product]()
我想要一个包含多个部分的 tableView,包括 headers。我添加 headers 没问题,没有错误;但是当它到达 numberofrows 时,它会给我一个错误。
这是我的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).
我发现我上面的代码没有任何问题。我在其他地方遇到了 updating/adding 产品到我的表格视图的问题,我没有添加到正确的部分。它只添加到第 0 节。所以上面的代码是正确的。
我有以下四个产品列表:
var products1 = [Product]()
var products2 = [Product]()
var products3 = [Product]()
var products4 = [Product]()
我想要一个包含多个部分的 tableView,包括 headers。我添加 headers 没问题,没有错误;但是当它到达 numberofrows 时,它会给我一个错误。
这是我的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).
我发现我上面的代码没有任何问题。我在其他地方遇到了 updating/adding 产品到我的表格视图的问题,我没有添加到正确的部分。它只添加到第 0 节。所以上面的代码是正确的。