为什么在点击按钮时隐藏一个标题部分

时间:2019-03-05 11:44:46

标签: ios swift uitableview uitableviewsectionheader

我有一个UITableView,它也有标题部分。根据标题部分,它们具有对应的数据。

  

每个标题部分都有一个按钮,用户可以单击该按钮   单击它会消耗UITableView

enter image description here

  

我正在使用以下代码

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
      let headerCell = Bundle.main.loadNibNamed("DashTableViewCell", owner: self, options: nil)?.first as! DashTableViewCell
    headerCell.lblQueryType.text = queriesArray[section]
    headerCell.btnExpendable.addTarget(self, action: #selector(expendSection), for: .touchUpInside)
    headerCell.btnExpendable.tag = section
    return headerCell
}

@objc func expendSection(button: UIButton) {
    var indexPaths = [IndexPath]()
    let section = button.tag
    if button.tag == 0 {
        QueriesStructArray[section].isExpended = !QueriesStructArray[section].isExpended
        for row in QueriesStructArray.indices {
            let indexPath = IndexPath(row: row, section: section)
            indexPaths.append(indexPath)
        }
    } else if button.tag == 1 {
        MyQueriesStructArray[section].isExpended = !MyQueriesStructArray[section].isExpended
        for row in MyQueriesStructArray.indices {
            let indexPath = IndexPath(row: row, section: section)
            indexPaths.append(indexPath)
        }
    }
    if button.tag == 1 {
        if MyQueriesStructArray[section].isExpended == true {
          tblProjectDashBoard.deleteRows(at: indexPaths, with: .fade)
        } else {
            tblProjectDashBoard.insertRows(at: indexPaths, with: .fade)
        }
    }
    else if button.tag == 0 {
        if QueriesStructArray[section].isExpended == true {
        tblProjectDashBoard.deleteRows(at: indexPaths, with: .fade)
        } else {
            tblProjectDashBoard.insertRows(at: indexPaths, with: .fade)
        }
    }
}

func numberOfSections(in tableView: UITableView) -> Int {
    return queriesArray.count
}
 func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 40
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0 {

        if QueriesStructArray[section].isExpended == true {
            return 0
        }
        return QueriesStructArray.count
    } else if section == 1 {
        if MyQueriesStructArray[section].isExpended == true {
            return 0
        } else {
        return MyQueriesStructArray.count
        }
      }
    return 0
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if let cell = tableView.dequeueReusableCell(withIdentifier: "ProjectDashTableViewCell", for: indexPath) as? ProjectDashTableViewCell {
        if indexPath.section == 0 {
            cell.lblTitle.text = QueriesStructArray[indexPath.row].queries
            return cell
        } else if indexPath.section == 1 {
            cell.lblTitle.text = MyQueriesStructArray[indexPath.row].myQueries
            return cell
        }
    }
    return UITableViewCell()
}
  

一切正常,但是当我单击“共享查询”时   按钮,然后“我的查询”部分隐藏了,这是错误的,我无法   在代码中找到问题

enter image description here

0 个答案:

没有答案