swift - 为什么我的 UIViewController 没有出现在我的弹出式卡片中?

标签 swift rx-swift

我想为我的 UIViewController 创建一个弹出窗口并找到了这个 repo在 GitHub 上。

我的 InfoViewController 工作正常,它只有 4 个 UILabels(我认为这可能是当您使用可重复使用的单元格时它没有出现的问题) 但不知何故,它不适用于我的 StructureNavigationListViewController,我不知道为什么。

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


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

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

在我的StructureNavigationListViewController中,我设置了 TableView 和弹出窗口:

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() 方法吗?

最佳答案

很难说出问题是什么,因为您没有留下关于在哪里调用 didTapCategory 的信息,但也许它与您的 modelSelected 有关订阅被提前释放?

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

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

关于swift - 为什么我的 UIViewController 没有出现在我的弹出式卡片中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58399322/

相关文章:

json - 将 'pretty printed' JSON 字符串转换为紧凑格式表示的最简单方法是什么?

swift - Realm ,将字符串保存\更新\观察到 Realm 对象的最佳方法

ios - RxDataSources 和改变模型的能力

ios - 无效的二进制文件 - 无效的 swift 支持

ios - 将结果从 Swift 数组传递到 Segue 到描述

ios - RxSwift 中可观察集合中的平面映射可观察属性

swift - 如何使用 RxSwift 订阅数组变化?

ios - 如何测试 RxSwift 变量和 RxCocoa Observable 之间的 UI 绑定(bind)?

ios - 添加 UIView 容器后出现意外的 subview

swift - 如何在 swift 4 中实现 CICheckerboardGenerator?