如何在表视图中实现另一个级别?

时间:2017-09-06 07:05:42

标签: ios uitableview swift3

这里我实现了可折叠表视图,其中第一级可以用作段,子类可以作为行计数给出,而另一个子类如何在表视图中实现,我有一个展开按钮,如果我点击它需要展开并显示第三级,图像显示在库下面,任何人都可以帮我解决这个问题吗?

enter image description here

这里显示的是我已经实现的代码

 func numberOfSections(in tableView: UITableView) -> Int {
        return detailsArray.count
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return detailsArray[section].childern.count
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = UIView(frame: CGRect(x: 275, y: 5, width: 30, height: 35))
        headerView.backgroundColor = UIColor.white
        headerView.tag = section
        let headerString = UILabel(frame: CGRect(x: 10, y: 10, width: tableView.frame.size.width-10, height: 30)) as UILabel
        headerString.text = detailsArray[section].name
        headerView .addSubview(headerString)
        let btn :UIButton = UIButton(frame: CGRect(x: UIScreen.main.bounds.size.width - 35, y: 5, width: 30, height: 35))
        btn.backgroundColor = UIColor.white
        if (detailsArray[section].childern.count != 0){
            btn.setImage(plusImage , for: .normal)
        }
        else {
            btn.setImage(UIImage(named: ""), for: .normal)
        }
        if detailsArray[section].expanded == true {
            btn.setImage(minusImage, for: .normal)
        }
        else {
            btn.setImage(plusImage, for: .normal)
        }
        btn.tag = section
        btn.addTarget(self, action: #selector(buttonTapped(sender:)), for: .touchUpInside)
        headerView.addSubview(btn)
        let headerTapped = UITapGestureRecognizer (target: self, action:#selector(sectionHeaderTapped(recognizer:)))
        headerView .addGestureRecognizer(headerTapped)
        return headerView
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        activityIndicator.stopAnimating()
        activityIndicator.hidesWhenStopped = true
        tableView.isHidden = false
        let cell = tableView.dequeueReusableCell(withIdentifier: "categoriescell", for: indexPath) as! CategoriesTableViewCell
        let obj1 = self.detailsArray[indexPath.section].childern[indexPath.row]
        cell.categoryLabel?.text = obj1.name
        cell.expandButton.tag = indexPath.row
        cell.expandButton.setImage(plusImage, for: .normal)
        return cell
    }
    func sectionHeaderTapped(recognizer: UITapGestureRecognizer) {
        let newViewcontroller = storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController
        self.navigationController?.pushViewController(newViewcontroller, animated: true)
    }
    func buttonTapped(sender: UIButton) {
        let section = sender.tag
        detailsArray[section].expanded = !detailsArray[section].expanded
        tableView.beginUpdates()
        for i in 0 ..< detailsArray[section].childern.count {
            tableView.reloadRows(at: [IndexPath(row: i, section: section)], with: .automatic)
        }
        if (detailsArray[section].childern.count != 0) {
            if (sender.currentImage == plusImage){
                sender.setImage(minusImage, for: .normal)
            }
            else {
                sender.setImage(plusImage, for: .normal)
            }
        }
        else {
            sender.setImage(UIImage(named: ""), for: .normal)
        }
        tableView.endUpdates()
    }

0 个答案:

没有答案