Swift 3: contrae otros UITableViewCell cuando la celda actual se expande

Soy un desarrollo de iOS más fresco. Estoy tratando de expandir mi UITableViewCell y puedo expandir las células. Pero quiero queuepsar UITableViewCell que no están seleccionados.

Lo que estoy intentando en el código: –

 var expandedCells = [Int]() @IBOutlet weak var tableVieww:UITableView! @IBAction func buttonPressed(_ sender: AnyObject) { // If the array contains the button that was pressed, then remove that button from the array if expandedCells.contains(sender.tag) { expandedCells = expandedCells.filter({ $0 != sender.tag}) } // Otherwise, add the button to the array else { expandedCells.append(sender.tag) } // Reload the tableView data anytime a button is pressed tableVieww.reloadData() } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 20 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! exampleCell cell.myButton.tag = indexPath.row return cell } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { // Set the row height based on whether or not the Int associated with that row is contained in the expandedCells array if expandedCells.contains(indexPath.row) { return 212 } else { return 57 } } 

Por favor, ayúdame.

Puede mantener una variable para mantener el índice seleccionado como se muestra a continuación,

 var expandedIndexPath: IndexPath? 

Luego, actualice su delegado de tableView de la siguiente manera,

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { expandedIndexPath = indexPath // Update the expandedIndexPath with the selected index tableView.reloadData() // Reload tableview to reflect the change } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { // Check wether expanded indexpath is the current index path and updated return the respective height if expandedIndexPath == indexPath { return 212 } else { return 57 } } 

Esto debería funcionar bien.

en # swift 2.3 Espero que esto también sea útil Para queuepsar y expandir el encabezado

defina la variable por encima de viewDidLoad o dentro del bloque de class

 var headerIcon: UIImageView = UIImageView() var sectionTitleArray : NSMutableArray = NSMutableArray() var sectionContentEventTitleDict : NSMutableDictionary = NSMutableDictionary() var sectionEventImagesDict : NSMutableDictionary = NSMutableDictionary() var arrayForBool : NSMutableArray = NSMutableArray() var arrFirstSectionItemTitle = ["Section 1 item Name 1","Section 1 item Name 2","Section 1 item Name 3","Section 1 item Name 4","Section 1 item Name 5","Section 1 item Name 6","Section 1 item Name 7"] var arrFirstSectionItemImages = ["Section_1_icon_1","Section_1_icon_2","Section_1_icon_3","Section_1_icon_4","Section_1_icon_5","Section_1_icon_6","Section_1_icon_7"] var arrSecondSectionItemTitle = ["Section 2 item Name 1","Section 2 item Name 2","Section 2 item Name 3","Section 2 item Name 4"] var arrSecondSectionItemImages = ["Section_1_icon_1","Section_2_item_icon_2","Section_2_item_icon_3","Section_1_item_icon_4","Section_1_item_icon_5","Section_1_item_icon_6","Section_1_item_icon_7"] var arrData: Array<Dictionary<String,AnyObject>> = Array<Dictionary<String,AnyObject>>() @IBOutlet weak var tableList: UITableView! 

en el método viewDidLoad () escriba estas líneas de código

 tableList.delegate = self tableList.dataSource = self tableList.backgroundColor = UIColor.getRGBColor(240, g: 240, b: 240) let customView = UIView(frame: CGRectMake(0, 0, 200, 80)) customView.backgroundColor = UIColor.getRGBColor(240, g: 240, b: 240) let customLine = UIView(frame: CGRectMake(0, 0, tblAllQuestion.frame.size.width, 1)) customLine.backgroundColor = UIColor.getRGBColor(240, g: 240, b: 240) customView.addSubview(customLine) tableList.tableFooterView = customView 

ahora escriba el delegado y los methods de fuente de datos de la siguiente manera: // MARK: UITableViewDelegate Y UITableViewDataSource MÉTODO CON SECCIÓN

para que el número de secciones se contraiga y expanda

 func numberOfSectionsInTableView(tableView: UITableView) -> Int { print_debug(sectionTitleArray.count) return sectionTitleArray.count } 

método celular:

  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("CELL_IDENTIFIER", forIndexPath: indexPath)as! CellAllQuestionFiqhVC //let cellIdentifier = "CellAllQuestionFiqhVC" // cell = self.tableList.dequeueReusableCellWithIdentifier(cellIdentifier) let manyCells : Bool = arrayForBool .objectAtIndex(indexPath.section).boolValue if (!manyCells) { // cell.textLabel.text = @"click to enlarge"; } else{ let group = self.arrData[indexPath.section] let data: Array<Dictionary<String,AnyObject>> = group["data"]as! Array<Dictionary<String,AnyObject>> cell.configCell(data[indexPath.row], instance: self) } return cell } 

celda de número en una sección:

 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if(arrayForBool.objectAtIndex(section).boolValue == true){ let count1 = arrData[section]["data"]! if count1.count != nil { return count1.count } } return 0; } 

el alto de la sección

 func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 40 } 

La altura de la celda:

 func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { if(arrayForBool.objectAtIndex(indexPath.section).boolValue == true){ return 100 } return 2; } 

para configurar el encabezado tappable:

 func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let headerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 40)) headerView.backgroundColor = UIColor.whiteColor()//Constant.kAPP_COLOR headerView.tag = section let headerString = UILabel(frame: CGRectMake(40/*tableView.frame.size.width/2-30*/, 10, tableView.frame.size.width, 18)) let headerLine = UIView(frame: CGRectMake(0, 39, tableView.frame.size.width, 1)) //UILabel(frame: CGRect(x: 20, y: 10, width: tableView.frame.size.width-10, height: 30)) as UILabel //headerLine.text = sectionTitleArray.objectAtIndex(section) as? String headerLine.backgroundColor = UIColor.getRGBColor(240, g: 240, b: 240) headerView .addSubview(headerLine) headerIcon = UIImageView(frame: CGRect(x: tblAllQuestion.bounds.maxX-30, y: 10, width: 8, height: 12)) as UIImageView headerIcon.image = UIImage(named: "right_arrow") headerView.addSubview(headerIcon) let headerTapped = UITapGestureRecognizer(target: self, action: #selector(sectionHeaderTapped)) headerView.addGestureRecognizer(headerTapped) return headerView } 

para rotar el ícono de la sección en el que se toca:

 func rotateSectionIconOnTapped(indexTapped: Int) { isActiveMap = false if arrayForBool.objectAtIndex(indexTapped) as! NSObject == true { print("toggle the icon at degree 90") headerIcon.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2)) } else if arrayForBool.objectAtIndex(indexTapped) as! NSObject == false { print("toggle the ico at degree 0") headerIcon.transform = CGAffineTransformMakeRotation(0) } } 

acción para el encabezado de sección tocado:

 func sectionHeaderTapped(recognizer: UITapGestureRecognizer) { //filterByCategoryIsOn = false print("Tapping working") print(recognizer.view?.tag) //if recognizer.view!.tag != 2 { let indexPath : NSIndexPath = NSIndexPath(forRow: 0, inSection:(recognizer.view?.tag as Int!)!) if (indexPath.row == 0) { for (indexx, bl) in arrayForBool.enumerate() { if recognizer.view!.tag != indexx { let index : NSIndexPath = NSIndexPath(forRow: 0, inSection:(indexx)) arrayForBool.replaceObjectAtIndex(index.section, withObject: false) let range = NSMakeRange(index.section, 1) let sectionToReload = NSIndexSet(indexesInRange: range) self.tableList.reloadSections(sectionToReload, withRowAnimation:UITableViewRowAnimation.Fade) } } var collapsed = arrayForBool.objectAtIndex(indexPath.section).boolValue print_debug(collapsed) collapsed = !collapsed; print_debug(collapsed) arrayForBool.replaceObjectAtIndex(indexPath.section, withObject: collapsed) print(arrayForBool) //reload specific section animated let range = NSMakeRange(indexPath.section, 1) let sectionToReload = NSIndexSet(indexesInRange: range) self.tableList.reloadSections(sectionToReload, withRowAnimation:UITableViewRowAnimation.Fade) rotateSectionIconOnTapped(recognizer.view!.tag) } } 

Obtenga acción para la fila de selección de datos en la ruta del índice:

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { print_debug(indexPath) print_debug(indexPath.row) print_debug(indexPath.section) if indexPath.section == 0 { } else if indexPath.section == 1 { } }