展开当前单元格时折叠其他UITableViewCell

我正在尝试扩展我的UITableViewCell ,我可以扩展单元格。 但我想要折叠未选中的UITableViewCell

我在代码中尝试的是:

 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 } } 

您可以维护一个变量来维护所选索引,如下所示,

 var expandedIndexPath: IndexPath? 

然后更新tableView委托,如下所示,

 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 } } 

这应该工作正常。

1)首先创建Bool数组变量

 var isExpandArr = Array() 

2)在ViewLoad()中的0索引处插入true

 isExpandArr.insert(true, at: 0) 

3)放入cellForRowAt

 cell.ToggleBT.addTarget(self, action: #selector(handleToggleBtn), for:.touchUpInside) cell.ToggleBT.tag = indexPath.row if isExpandArr[indexPath.row] == true{ cell.bottomrView.isHidden = false }else{ cell.bottomrView.isHidden = true } 

4)设置heightForRowAt

 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return isExpandArr[indexPath.row] ? 204 : 60 } 

5)放function

 func handleToggleBtn(sender: UIButton){ print(sender.tag) if isExpandArr.contains(true){ print("yes") if let unwrappedIndex = isExpandArr.index(of: true){ isExpandArr[unwrappedIndex] = false isExpandArr[sender.tag] = true } } table.reloadData() } 

在#swift 2.3希望这也有用于崩溃和扩展标题

在viewDidLoad之上或类块中定义变量

 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> = Array>() @IBOutlet weak var tableList: UITableView! 

在viewDidLoad()方法中编写这些代码行

 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 

现在编写委托和数据源方法如下:// MARK:UITableViewDelegate和UITableViewDataSource WITH WITH SECTION

对于要折叠和展开的部分数量

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

细胞方法:

  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> = group["data"]as! Array> cell.configCell(data[indexPath.row], instance: self) } return cell } 

部分中的数字单元格:

 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; } 

部分的高度

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

细胞的高度:

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

设置标头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 } 

用于旋转部分图标点击:

 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) } } 

节目标题的操作:

 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) } } 

获取索引路径上的didselect行的操作:

 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 { } }