首页 > 解决方案 > 如何在 tableView Swift 中使 Xib 页脚项目可点击

问题描述

我正在努力使我的页脚项目在 tableView 中可点击。在 tableView 中,我使用了两个不同的 Xib,并tableView - didSelect function在单击时用作可扩展单元格。

1) 主要 Xib

2) Child_View - 页脚 Xib

现在在作为 childView 的页脚项目上 - 我也想进行可点击的操作,所以点击它会navigate to viewController显示他们的详细信息。

附上三张图片

宣布 :var selectedIndex : Int! = -1

代码:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == selectedIndex{
            selectedIndex = -1

        }else{
            selectedIndex = indexPath.row
        }
        tableView.reloadData()
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.row == selectedIndex
        {
            return UITableView.automaticDimension
        }else{
            return 71
        }
    }

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

        return 1 
    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "DownloadEntryViewCell", for: indexPath) as! DownloadEntryViewCell
        let dic = Appdata?[indexPath.row]
        var stackHeight:CGFloat = 0.0
        cell.fileNameLabel.text = dic?.date

        for i in Appdata ?? [] {
            let child_view = Bundle.main.loadNibNamed("FooterView", owner: self, options: nil)?.first as! FooterView
            child_view.projName.text = "\(i.projectName ?? "")" + "  " + "\(i.subTitle ?? "")"

            cell.stackViewFooter.addArrangedSubview(child_view)
            stackHeight = stackHeight + 60.0

        }
        cell.stackViewFooter.heightAnchor.constraint(equalToConstant: stackHeight).isActive = true
        return cell
    }

图片:

主要 可展开可 点击

标签: iosswiftuitableviewxib

解决方案


推荐阅读