ios - How to properly insert new row in correct specific row cell in the table view (swift)? -
so here question..
i trying insert row in index add button inside row..
example : click add button @ row 1, should add new row below row, if click add button @ row 3 should add new row below it..
the problem after add example 5 new row, if click add in row 5 (this new row added new add button inside it) keep adding below last row clicked.. (i click add on row 3 has new row below it, click add on new row keep adding below row 3)
steps in gif :
- i add new first row adding new row below it.. (correct behavior)
- i try add new row in last row keep adding new row below row 3. (wrong behavior)
- i try click again add row in row 4 correctly add new row below it, , click on row 3 , add correctly new row below it. (correct behavior)
why keep problem in number 2 ?
here code :
the problem in case ingredient , step.. give rowid in cell using indexpath each cell has own 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() }
this 1 delegate handler create pass cell click add button
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) , cell id \(cellid)") }
the last 1 cell itself
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 1 : ", cellid) if item?.type == .ingredient { delegate?.addednewcell(insection: 2, cellid: cellid! + 1) }else{ delegate?.addednewcell(insection: 3, cellid: cellid! + 1) } } }
here gif , number of step information..
or real low quality gif here
Comments
Post a Comment