在第一次模拟器启动时,ViewController将无法正确重新加载

时间:2016-04-26 00:39:14

标签: ios swift uicollectionview

我有一个奇怪的问题,我刚刚在模拟器上重新安装并启动我的项目时发现了

我的主ViewController有一个集合视图,它将显示一个食谱列表。添加配方并导航回此屏幕后,列表将反映更新。 当recipe.count > 0子视图包含按钮(按类别过滤列表)时,还会出现ScrollView。 scrollview反映了配方的更新,但它在Simulator中首次启动时无效。

我已经尝试重新加载collectionview,在viewDidAppear,viewWillAppear上获取配方数据但是第一次启动没有运气。

如何确保第一次应用发布体验与后续发布时相同?首次在模拟器上启动时是否有任何已知问题我应该知道?

enter image description here

以下是创建按钮并添加到ScrollView的代码。

override func viewDidAppear(animated: Bool) {

    super.viewDidAppear(animated)
    fetchRecipe()
    collection.reloadData()

    if recipes.count == 0 {
        categoryScrollView.hidden = true
        categoryScrollViewHeight.constant = 0
    } else {
        populateCategoryScrollView()
    }

}


func populateCategoryScrollView() {

    //Create category sort buttons based on fetched category.name and add to scrollview
    if recipes.count > 0 {

        var categories: [String] = []

        for recipe in recipes {
            if let value = recipe.category {
                let category = value as! RecipeCategory
                categories.append(category.name!)
            }
        }

        var distinctCategories = Array(Set(categories))
        var widthStack = 0

        if distinctCategories.count != 0 {

            for subView in categoryScrollView.subviews {
                subView.removeFromSuperview()
            }

            for i in 0..<distinctCategories.count {
                //Pilot button creation to get width
                let frame1 = CGRect(x: 0, y: 6, width: 80, height: 40 )
                let button = UIButton(frame: frame1)
                button.setTitle("\(distinctCategories[i])", forState: .Normal)
                button.sizeToFit()

                let buttonWidth = Int(button.frame.size.width)
                var frame2: CGRect

                if i == 0 {
                    frame2 = CGRect(x: 10, y: 6, width: buttonWidth+20, height: 30 )
                } else {
                    frame2 = CGRect(x: 10 + widthStack, y: 6, width: buttonWidth+20, height: 30 )
                }

                widthStack += buttonWidth+32

                let button1 = UIButton(frame: frame2)
                let attributedTitle = NSAttributedString(string: "\(distinctCategories[i])".uppercaseString, attributes: [NSKernAttributeName: 1.0, NSForegroundColorAttributeName: UIColor.blackColor()])
                button1.setAttributedTitle(attributedTitle, forState: .Normal)
                button1.titleLabel!.font = UIFont(name: "HelveticaNeue", size: 13.0)
                button1.layer.borderWidth = 2.0
                button1.layer.borderColor = UIColor.blackColor().CGColor
                button1.backgroundColor = UIColor.whiteColor()
                button1.addTarget(self, action: "filterByCategory:", forControlEvents: UIControlEvents.TouchUpInside)
                self.categoryScrollView.addSubview(button1)
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

首次进入应用程序时,recipes.count为0,因此您将通过约束categoryScrollViewHeight.constant = 0隐藏滚动视图。因此,下次创建新配方时,滚动视图的高度仍为零。这就是为什么你没有在第一次发布时看到滚动。