在单元格内扩展表格视图单元格

时间:2020-03-13 21:01:49

标签: uitableview

im试图在viewCell内制作一个可扩展的表格视图

export default function Dashboard() {
  const {user, isLoggedIn} = useContext(AuthContext)

  if (isLoggedIn){
    return (
      <div className="Dashboard">El email es {user.email}</div>
     );
  } 

  return (<div>User is not logged in</div>);
}

您每次用户选择激活的某些单元格时都会看到

import UIKit

class TableSupportVC: UITableViewController {


    let interface: TableSupportView = {
           let interface = TableSupportView(frame: CGRect.zero)
           interface.translatesAutoresizingMaskIntoConstraints = false
           return interface
       }()


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

    struct dataInside {
        var opened = Bool()
        var title = String()
        var info = [String]()
    }

    var tableViewData = [cellData]()
    var tableViewDataInside = [dataInside]()

    override func viewDidLoad() {
        super.viewDidLoad()

        //register tables
        tableView.register(TableSupportCell.self, forCellReuseIdentifier: menuCellIdita)
        tableView.register(TableSupportSubtitleCell.self, forCellReuseIdentifier: menuCellda)

        // Heres my data for cells 
        tableViewData = [cellData(opened: false, title: "1", sectionData: ["1.1", "1.2","1.3","1.4"]),
            cellData(opened: false, title: "2", sectionData: ["2.1", "2.2", "2.3"])
        ]



        tableViewDataInside = [dataInside(opened: false, title: "1.1.1", info: ["1.1.1"])]


        setThemeNavigationBar()
                // Size Scrollview
        interface.scrollView.contentSize = CGSize(width: wScreen, height: hScreen * 1.40)

                initComponents()



    }



    func setThemeNavigationBar() {

        let navigationBarAppearace = UINavigationBar.appearance()
        navigationBarAppearace.tintColor = blueBlumon
        navigationBarAppearace.barTintColor = blueBlumon
        navigationController?.navigationBar.barTintColor = blueBlumon
        navigationController?.navigationBar.barStyle = .blackOpaque
        navigationController?.navigationBar.isTranslucent = false

    }


    private func initComponents() {

        setNavItems()
        setSubviews()
        setAutolayout()


    }


    private func setSubviews() {
        view.backgroundColor = UIColor.white
        view.addSubview(interface)
    }



    private func setNavItems() {

        let backIcon = UIImage(named: "whiteArrow")
        let backItem = UIBarButtonItem(image: backIcon?.withRenderingMode(.alwaysTemplate), style: UIBarButtonItem.Style.plain, target: self, action: #selector(backPressed))
        backItem.tintColor = UIColor.white
        navigationItem.leftBarButtonItem = backItem

        let logoIcon = UIImage(named: "itemLogoBlue")
        let logoItem = UIBarButtonItem(image: logoIcon?.withRenderingMode(.alwaysOriginal), style: UIBarButtonItem.Style.plain, target: nil, action: nil)
        navigationItem.rightBarButtonItem = logoItem

    }

    // Método para definir el autolayout de los componentes de la vista principal del controlador
    private func setAutolayout() {

        NSLayoutConstraint.activate([
            interface.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
            interface.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            interface.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
            interface.leadingAnchor.constraint(equalTo: view.leadingAnchor)
            ])

    }

    // Método de selector backPressed
    @objc private func backPressed() {
        self.navigationController?.popViewController(animated: true)
        dismiss(animated: true, completion: nil)
    }
    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return tableViewData.count
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {


        if tableViewData[section].opened == true{
            return tableViewData[section].sectionData.count + 1
        }

             **//INDEX OUT OF RANGE HERE**
            if tableViewDataInside[section].opened == true{
                return tableViewDataInside[section].info.count + 1
            }
        else {
            return 1
        }
        }



    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        if indexPath.row == 0{
            let cell = tableView.dequeueReusableCell(withIdentifier: menuCellIdita, for: indexPath) as! TableSupportCell
            cell.optionTitle.text = tableViewData[indexPath.section].title

            return cell

        }
        if indexPath.row == 2 && tableViewData[indexPath.section].title == "1.2"{
                let cell = tableView.dequeueReusableCell(withIdentifier: menuCellda, for: indexPath) as! TableSupportSubtitleCell
                cell.title.text = tableViewDataInside[indexPath.section].title
                //cell.subtitle.text = tableViewDataInside[indexPath.section].info
                       return cell
            }

        else {

            let cell = tableView.dequeueReusableCell(withIdentifier: menuCellIdita, for: indexPath) as! TableSupportCell
            cell.optionTitle.text = tableViewData[indexPath.section].sectionData[indexPath.row - 1]

            return cell
        }
    }







    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("el indexpath es: \(indexPath.row)")
        if indexPath.row == 0 {
            if tableViewData[indexPath.section].opened == true{
                tableViewData[indexPath.section].opened = false
                let sections = IndexSet.init(integer: indexPath.section)
                tableView.reloadSections(sections, with: .none)


            }

            else{
                tableViewData[indexPath.section].opened = true
                let sections = IndexSet.init(integer: indexPath.section)
                tableView.reloadSections(sections, with: .none)

            }


        }

        if indexPath.row == 2 && tableViewData[indexPath.section].title == "1.2"{
            if tableViewDataInside[indexPath.section].opened == true{
                tableViewDataInside[indexPath.section].opened = false
                let sections = IndexSet.init(integer: indexPath.section)
                tableView.reloadSections(sections, with: .none)


            }

            else{
                tableViewDataInside[indexPath.section].opened = true
                let sections = IndexSet.init(integer: indexPath.section)
                tableView.reloadSections(sections, with: .none)

            }


        }
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80
    }



}

如果用户按第0行,它将激活代码

相同

覆盖func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath)-> UITableViewCell {

但是当我尝试设置

重写func tableView(_ tableView:UITableView,numberOfRowsInSection部分:Int)-> Int {

并使其崩溃索引超出范围

主要问题是无法像im做扩展一样在另一个单元中归档信息,我想再次归档 tableViewDataInside 这是我的信息,但是我可以使它工作

0 个答案:

没有答案
相关问题