为什么我的UIViewController没有显示在弹出卡中?

时间:2019-10-15 16:48:20

标签: swift rx-swift

我想为我的 UIViewController 之一创建一个弹出窗口,并在GitHub上找到此repo

它与我的 InfoViewController 正常运行,而后者只有4个 UILabels (我认为这可能是使用可重复使用的单元时未显示的问题) 但是以某种方式它不能与我的 StructureNavigationListViewController 一起使用,我也不知道为什么。

我在我的 MainViewController 中调用 didTapCategory 方法,但应在其中弹出StructureNavigationController,但是我只看到变暗视图(这很奇怪,因为点击识别器和平移手势可以正常工作,但没有内容显示)


在我的 MainViewController 中,我像以前一样设置了弹出窗口:

    @IBAction func didTapCategory(_ sender: UIBarButtonItem) {
        let popupContent = StructureNavigationListViewController.create()
        let cardpopUp = SBCardPopupViewController(contentViewController: popupContent)
        cardpopUp.show(onViewController: self)
    }

在我的 StructureNavigationListViewController 中,设置表格视图并弹出:

public var popupViewController: SBCardPopupViewController?

    public var allowsTapToDismissPopupCard: Bool = true

    public var allowsSwipeToDismissPopupCard: Bool = true

    static func create() -> UIViewController {
        let sb = UIStoryboard(name: "Main", bundle: nil)
        let vc = sb.instantiateViewController(withIdentifier: "StructureNavigationListViewController") as! StructureNavigationListViewController
        return vc
    }

    @IBOutlet var tableView: UITableView!
    var structures = Variable<[Structure]>([])
    public var treeSource: StructureTreeSource?
    let disposeBag = DisposeBag()

    var depthDictionary : [String : Int] = [:]

    public override func viewDidLoad() {
        structures.asObservable()
            .bind(to:tableView.rx.items) {(tableView, row, structure) in
                let cell = tableView.dequeueReusableCell(withIdentifier: "StructureNavigationCell", for: IndexPath(row: row, section: 0)) as! StructureNavigationCell
                cell.structureLabel.text = structure.name
                cell.spacingViewWidthConstraint.constant = 20 * CGFloat(self.depthDictionary[structure.id]!)
                return cell
            }.disposed(by:disposeBag)

        _ = tableView.rx.modelSelected(Structure.self).subscribe(onNext: { structure in
            let storyBoard = UIStoryboard(name:"Main", bundle:nil)
            let plansViewCtrl = storyBoard.instantiateViewController(withIdentifier: "PlansViewController2") as! PlansViewController2
            self.treeSource?.select(structure)
            plansViewCtrl.treeSource = self.treeSource
            plansViewCtrl.navigationItem.title = structure.name
            self.show(plansViewCtrl, sender: self)
            if let mainVC = self.parent as? ProjectOverViewTabController2 {
                mainVC.addChildView(viewController: plansViewCtrl, in: mainVC.scrollView)
            }
        })
        showList()
    }
func showList() {
        if treeSource == nil {
            treeSource = StructureTreeSource(projectId:GlobalState.selectedProjectId!)
        }

        //The following piece of code achieves the correct order of structures and their substructures.
        //It is extremely bad designed and rather expensive with lots of structures and should
        //therefore be refactored!
        if let strctrs = getStructures() {
            var sortedStructures : [Structure] = []
            while(sortedStructures.count != strctrs.count) {
                for strct in strctrs {
                    if let _ = sortedStructures.index(of: strct) {
                        continue
                    } else {
                        depthDictionary[strct.id] = getDepthOfNode(structure: strct, depth: 1)
                        if let structures = getStructures() {
                            if let parent = structures.first(where: {$0.id == strct.parentId}) {
                                if let index = sortedStructures.index(of: parent) {
                                    sortedStructures.insert(strct, at: index+1)
                                }
                            } else {
                                sortedStructures.insert(strct, at: 0)
                            }
                        }
                    }
                }
            }
            structures.value = sortedStructures

            tableView.reloadData()
        }
    }

    func getDepthOfNode(structure: Structure, depth: Int) -> Int {
        if(structure.parentId == nil || structure.parentId == "") {
            return depth
        } else {
            if let structures = getStructures() {
                if let parent = structures.first(where: {$0.id == structure.parentId}) {
                    return getDepthOfNode(structure: parent, depth: depth + 1)
                }
            }
        }
        return -1
    }

    private func getStructures() -> Results<Structure>? {
        do {
            if let projectId = GlobalState.selectedProjectId {
                return try Structure.db.by(projectId: projectId)
            }
        } catch { Log.db.error(error: error) }

        return nil
    }
}


很多代码在这里。抱歉。

是因为在 viewDidLoad() 出队后,我调用了 create() 方法吗?

1 个答案:

答案 0 :(得分:1)

很难说出问题出在哪里,因为您没有留下应该调用didTapCategory的任何信息,但是也许与您的modelSelected订阅过早释放有关? / p>

编辑: 如此处发布的内容:https://stackoverflow.com/a/28896452/11851832,如果您的自定义单元格是使用Interface Builder构建的,则应该注册Nib,而不是类:

tableView.registerNib(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "CustomCellIdentifier")