所以这是我的问题..
我试图在某个索引中插入一行,并在该行中添加按钮..
示例:我单击第1行的添加按钮,然后它应该在该行下面添加新行,如果我点击第3行的添加按钮,它应该在它下面添加新行..
问题是在我添加示例5新行之后,有时如果我在第5行中单击添加(这是添加了新添加按钮的新行),它会继续添加到单击的最后一行...(我点击在第3行添加然后它下面有新行,然后我点击新行继续添加它继续添加到第3行
我的GIF中的步骤:
为什么我一直在第2号遇到问题?
这是我的代码:
问题在于成分和步骤..我已经使用indexPath在单元格中给出了rowId,因此每个单元格都有自己的id。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let item = items[indexPath.section]
switch item.type {
case .title:
if let cell = tableView.dequeueReusableCell(withIdentifier: PopUpTitleCell.identifier, for: indexPath) as? PopUpTitleCell {
return cell
}
case .category:
if let cell = tableView.dequeueReusableCell(withIdentifier: PopUpCategoryCell.identifier, for: indexPath) as? PopUpCategoryCell {
return cell
}
case .ingredient:
if let item = item as? PopUpModelIngredient, let cell = tableView.dequeueReusableCell(withIdentifier: PopUpIngredientStepCell.identifier, for: indexPath) as? PopUpIngredientStepCell {
cell.cellId = indexPath.row
cell.item = item
cell.delegate = self
return cell
}
case .step:
if let item = item as? PopUpModelStep, let cell = tableView.dequeueReusableCell(withIdentifier: PopUpIngredientStepCell.identifier, for: indexPath) as? PopUpIngredientStepCell {
cell.cellId = indexPath.row
cell.item = item
cell.delegate = self
return cell
}
case .post:
if let cell = tableView.dequeueReusableCell(withIdentifier: PopUpPostCell.identifier, for: indexPath) as? PopUpPostCell {
return cell
}
}
return UITableViewCell()
}
这是我创建的委托处理程序,用于传递单击添加按钮的单元格
func addedNewCell(inSection: Int, cellId: Int) {
popUpTableView?.beginUpdates()
let item = items[inSection]
if let item = item as? PopUpModelIngredient {
item.ingredientsCell += 1
popUpTableView?.insertRows(at: [IndexPath(row: cellId, section: inSection)], with: .automatic)
}else if let item = item as? PopUpModelStep {
item.stepsCell += 1
popUpTableView?.insertRows(at: [IndexPath(row: cellId, section: inSection)], with: .automatic)
}
popUpTableView?.endUpdates()
print("added in section \(inSection) and cell id \(cellId)")
}
最后一个是细胞本身
class PopUpIngredientStepCell: UITableViewCell {
var cellId: Int?
var delegate: CellAddedDelegate?
var item: PopUpViewModelItem?
static var nib:UINib {
return UINib(nibName: identifier, bundle: nil)
}
static var identifier: String {
return String(describing: self)
}
@IBAction func addMoreCell(_ sender: Any) {
print("Cell id for this one is : ", cellId)
if item?.type == .ingredient {
delegate?.addedNewCell(inSection: 2, cellId: cellId! + 1)
}else{
delegate?.addedNewCell(inSection: 3, cellId: cellId! + 1)
}
}
}
这是GIF和步骤信息的数量..
或真正的低质量gif