ios - Only Single cell expands at a time in UITableViewCell in swift3 -
i implementing expandable , collapsable table cell on click of button in custom table cell. have tried following code expands single cell @ time. if click cell expands if click on cell expands , expanded cell collapses.
var selectedindexpath: indexpath? func configure(cell: myproposalcustomcell, forrowatindexpath indexpath: indexpath) { let pool = myproposalsdetails[indexpath.row] cell.preservessuperviewlayoutmargins = false cell.separatorinset = uiedgeinsets.zero cell.layoutmargins = uiedgeinsets.zero cell.proposalnumber.text = pool.proposalnumber cell.pickuplocation.text = pool.pickuplocation cell.droplocation.text = pool.droplocation cell.journeytype.text = pool.journeytype cell.firstshiftonwardtime.text = pool.firstpickuptime cell.firstshiftreturntime.text = pool.firstendtime if let numberofinterests = pool.noofinterest { cell.numberofinterest.text = string(numberofinterests) } if let operatingdays = pool.operatingdays { cell.daysofoperation.attributedtext = utility.getproposedoperatingdays(operatingdays: operatingdays) } cell.expandcollapse.tag = indexpath.row cell.expandcollapse.addtarget(self, action: #selector(expandtablecell(sender:)), for: .touchupinside) if selectedindexpath == indexpath { uiview.animate(withduration: 0.3, animations: { cell.backgroundcolor = customcolor.selectedbackground.color cell.bottomview.backgroundcolor = customcolor.selectedbackground.color cell.expandcollapse.setimage(uiimage(named: "collapse_arrow.png"), for: .normal) if let proposedstartdate = pool.startdate { let propstartdate = date(timeintervalsince1970: proposedstartdate) cell.proposedstartdate.text = utility.getformatteddate(date: propstartdate) cell.proposedstartdatetxt.text = nslocalizedstring("proposed start date", comment: "") } cell.returntime.alpha = 0.0 }) } else { uiview.animate(withduration: 0.3, animations: { cell.backgroundcolor = uicolor.white cell.expandcollapse.setimage(uiimage(named: "down_arrow.png"), for: .normal) cell.proposedstartdatetxt.text = nslocalizedstring("journey type", comment: "") cell.bottomview.backgroundcolor = uicolor.white cell.proposedstartdate.text = pool.journeytype cell.returntime.ishidden = false }) } }
this expandable button action:
func expandtablecell(sender: uibutton) { let indexpath = indexpath(row: sender.tag, section: 0) if selectedindexpath == indexpath { selectedindexpath = nil } else { let previousselindex = selectedindexpath selectedindexpath = indexpath if let previousselectedindexpath = previousselindex { if tripstatus.triptype != .splitshift { if let previousselectedcell = myproposals.cellforrow(at: previousselectedindexpath) as? myproposalcustomcell { configure(cell: previousselectedcell, forrowatindexpath: previousselectedindexpath) } } else { if let previousselectedcell = myproposals.cellforrow(at: previousselectedindexpath) as? myproposalsplitshiftcell { configuresplitshift(cell: previousselectedcell, forrowatindexpath: previousselectedindexpath) } } } } updateselectedcell(indexpath: indexpath) myproposals.beginupdates() myproposals.endupdates() } func updateselectedcell(indexpath: indexpath) { if tripstatus.triptype != .splitshift { if let selectedcell = myproposals.cellforrow(at: indexpath) as? myproposalcustomcell { configure(cell: selectedcell, forrowatindexpath: indexpath) } } else { if let selectedcell = myproposals.cellforrow(at: indexpath) as? myproposalsplitshiftcell { configuresplitshift(cell: selectedcell, forrowatindexpath: indexpath) } } } func tableview(_ tableview: uitableview, heightforrowat indexpath: indexpath) -> cgfloat { let headingheight: cgfloat = 128 let detailsheight: cgfloat = 80 let splitshiftdetailsheight: cgfloat = 215 switch tripstatus.triptype { case .onewaytrip, .roundtrip: if selectedindexpath != nil && indexpath.compare(selectedindexpath! indexpath) == comparisonresult.orderedsame { return headingheight + detailsheight } return headingheight case .splitshift: if selectedindexpath != nil && indexpath.compare(selectedindexpath! indexpath) == comparisonresult.orderedsame { return headingheight + splitshiftdetailsheight } return headingheight } }
i want multiple cells expanded. how achieve this?
this straight forward, instead of taking selectedindexpath try keep 1 variable in custom uitableviewcell class like,
class expandablecell: uitableviewcell { var isexpanded: bool = false override func awakefromnib() { super.awakefromnib() // initialization code } override func setselected(_ selected: bool, animated: bool) { super.setselected(selected, animated: animated) // configure view selected state } @ibaction func morebuttontapped(_ sender: any) { } }
and try toggle isexpanded boolean flag, , try perform func updateselectedcell(indexpath: indexpath) {}
action.
Comments
Post a Comment