Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在点击其他分区时折叠打开的TableView分区_Ios_Swift - Fatal编程技术网

Ios 如何在点击其他分区时折叠打开的TableView分区

Ios 如何在点击其他分区时折叠打开的TableView分区,ios,swift,Ios,Swift,我使用的是可折叠的TableView部分,它工作正常,一切正常。但当其他未打开的部分点击时,我需要折叠已经打开的部分 struct CustomCell { var opened = Bool() var title = String() var sectionData = [String]() } // I'm using this part inside TableView didSelectForRowAt if tableViewData[in

我使用的是可折叠的TableView部分,它工作正常,一切正常。但当其他未打开的部分点击时,我需要折叠已经打开的部分

  struct CustomCell {
     var opened = Bool()
     var title = String()
     var sectionData = [String]()
  }

 // I'm using this part inside TableView didSelectForRowAt
 if tableViewData[indexPath.section].opened {
            tableViewData[indexPath.section].opened = false
            let section = IndexSet.init(integer: indexPath.section)
            tableView.reloadSections(section, with: .none)
        } else {
            tableViewData[indexPath.section].opened = true
            let section = IndexSet.init(integer: indexPath.section)
            tableView.reloadSections(section, with: .none)
        }

这是我项目的代码

 @objc private func sectionTapped(recognizer:UITapGestureRecognizer) {
        print("Tapped",recognizer.view?.tag)

        guard let tag = recognizer.view?.tag else {
            return
        }

        // We have already open section 
        if let currentExpandedTag = self.sectionExpanded {

            //collapse 
            if tag == currentExpandedTag {
                self.tableView.beginUpdates()
                self.sectionExpanded = nil

                self.tableView.reloadSections(IndexSet(integer: tag), with: .fade)
                self.tableView.endUpdates()
            } else {
                // Collapse current and expand other
                self.tableView.beginUpdates()
                self.tableView.reloadSections(IndexSet(integer: currentExpandedTag), with: .fade)

                self.sectionExpanded = tag
                self.tableView.reloadSections(IndexSet(integer: tag), with: .fade)

                self.tableView.endUpdates()
                self.tableView.scrollToRow(at: IndexPath(row: 0, section: tag), at: .top, animated: true)

            }
        } else {
            // Nothing expanded
            self.tableView.beginUpdates()
            self.sectionExpanded = tag

            self.tableView.reloadSections(IndexSet(integer: tag), with: .fade)


            self.tableView.endUpdates()
        }

    }
说明:

1) 在
func tableView(tableView:UITableView,viewForHeaderInSection:Int)->UIView?
方法中,我已将标记设置为内容标题视图,并添加了点击手势

2) 我在视图controller
var sectionExpanded:Int?
中有一个属性,它跟踪扩展的节

3) 其他代码是不言自明的

4) 在
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int
方法中,我检查了以下内容

   if let sectionExpanded = self.sectionExpanded, section == sectionExpanded {
        return subItemCount // From your array 
    }
    return 0