ios - Hide and Show TableViewCells properly? -
i in process of making table view controller app , has different headers. using custom header view has button inside it. button used show rows section. save section number of each header in tag property of button.
i have looked @ more 20 posts set height 0 , hide cell. ridiculous auto layout produce tons of errors , make app lag lot.
what did make 2 different cells. 1 used show data , other 1 empty cell height of 0.
then in code have variable called opensection , holds current open section number.
so when click on button current section closes , new 1 opens. there lots , lots of issues. instance if scroll cells keep changing. next problem fact if open last header, cannot scroll first header weird.
this function when button tapped
func buttontapped(_ sender: uibutton) { dispatchqueue.main.async { self.opensection = sender.tag self.tableview.reloaddata() self.tableview.beginupdates() self.tableview.endupdates() } print("button tapped") }
this cellforrowat function
if opensection == indexpath.section { print("cellforrowat") print(opensection) let cell = tableview.dequeuereusablecell(withidentifier: "treatmentcell", for: indexpath) as! treatmenttableviewcell cell.name.text = self.treatments[indexpath.section].treatments[indexpath.row].name cell.price.text = self.treatments[indexpath.section].treatments[indexpath.row].price cell.details.text = self.treatments[indexpath.section].treatments[indexpath.row].details return cell } else { let cell = tableview.dequeuereusablecell(withidentifier: "emptycell", for: indexpath) cell.ishidden = true return cell }
and heightforrowat function
if opensection == indexpath.section { print("heightforrowat") print(opensection) return self.tableview.rowheight } else { return 0 }
change code button tapped
self.opensection = sender.tag let index = indexpath(row: 0, section: opensection) dispatchqueue.main.async { self.tableview.reloaddata() self.tableview.reloadsections(indexset(integer: self.opensection), with: .automatic) self.tableview.scrolltorow(at: index, at: .top, animated: true) }
and change number of rows in section code below
override func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int { // #warning incomplete implementation, return number of rows if opensection == section { return self.treatments[opensection].treatments.count } else { return 0 } }
Comments
Post a Comment