在同一视图控制器上的AlertViewController上按OK按钮后重新加载集合视图?

时间:2018-02-01 12:52:09

标签: ios swift core-data uicollectionview uialertcontroller

如何在同一视图控制器上按下AlertViewController上的OK按钮后重新加载集合视图?

当我点击主屏幕然后再次返回此屏幕时,它会显示保存的数据?当我按下AlertController上的OK按钮时,我想显示这些数据。我不知道如何解决这个问题。

    override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(true)

            //Fetch Category Data
            categoryCoreData.fetchData()
            self.collCategory.reloadData()

}


func numberOfSections(in collectionView: UICollectionView) -> Int
    {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        return addCategory.count + 1
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {


        let cellID = indexPath.row < addCategory.count ? "CategoryCell" : "ExtraCell"

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath)

        setupCell(cell: cell, indexPath: indexPath, type: cellID)

        return cell
    }

    func setupCell(cell: UICollectionViewCell, indexPath: IndexPath, type: String)
    {
        switch(type)
        {
            case "CategoryCell":
                setupCategoryCell(cell: cell as! CategoryCollectionCell, indexPath: indexPath)
            case "ExtraCell":
                setupAddButtonCell(cell: cell as! CategoryExtraCell, indexPath: indexPath)
            default:
                break
        }
    }
func setupCategoryCell(cell: CategoryCollectionCell, indexPath: IndexPath)
    {
        let cat = addCategory[indexPath.row]
        cell.lblHeader.text = cat.category_name
    }

func setupAddButtonCell(cell: CategoryExtraCell, indexPath: IndexPath)
    {
        //Extra Button "Add Button" in a cell
    }


    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
    {
        if indexPath.item < addCategory.count
        {
            print("Main Cell")
        }
        else
        {
            print("Add New Cell")

            self.blurEffects()
            view.addSubview(blurEffectView)

            //Alert View Controller when Adding Categories...
            let inputBox = BMInputBox.boxWithStyle(.plainTextInput)
            inputBox.blurEffectStyle = .extraLight

            inputBox.title = NSLocalizedString("Add Category", comment: "")
            inputBox.message = NSLocalizedString("Please enter unique category name.", comment: "")

            inputBox.customiseInputElement = {(element: UITextField) in

                element.placeholder = "Enter a category"
                return element
            }

            inputBox.submitButtonText = NSLocalizedString("OK", comment: "")

            inputBox.onSubmit = {(value: String...) in

                //Store value in text field in "text" object.
                for text in value
                {
                    if text is String
                    {
                        //Store category in CoreData
                        categoryCoreData.saveData(tfCat: text)

                    }
                }
                self.blurEffectView.removeFromSuperview()

            }


            inputBox.cancelButtonText = NSLocalizedString("Cancel", comment: "")

            inputBox.onCancel = {
                //Remove blur effects from Superview
                self.blurEffectView.removeFromSuperview()
            }

            inputBox.show()
        }
    }

2 个答案:

答案 0 :(得分:2)

    let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.alert)
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: { action in

        self.collectionView.reloadData()

    }))
    self.present(alert, animated: true, completion: nil)

答案 1 :(得分:0)

要重新加载collectionView,请使用reloadData(),如下所示

self.collCategory.reloadData()

在您的cellForAtItem

 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
    {
        if indexPath.item < addCategory.count
        {
            print("Main Cell")
        }
        else
        {
            print("Add New Cell")

            self.blurEffects()
            view.addSubview(blurEffectView)

            //Alert View Controller when Adding Categories...
            let inputBox = BMInputBox.boxWithStyle(.plainTextInput)
            inputBox.blurEffectStyle = .extraLight

            inputBox.title = NSLocalizedString("Add Category", comment: "")
            inputBox.message = NSLocalizedString("Please enter unique category name.", comment: "")

            inputBox.customiseInputElement = {(element: UITextField) in

                element.placeholder = "Enter a category"
                return element
            }

            inputBox.submitButtonText = NSLocalizedString("OK", comment: "")

            inputBox.onSubmit = {(value: String...) in

                //Store value in text field in "text" object.
                for text in value
                {
                    if text is String
                    {
                        //Store category in CoreData
                        categoryCoreData.saveData(tfCat: text)

                    }
                }
                self.blurEffectView.removeFromSuperview()
                self.collCategory.reloadData()
            }


            inputBox.cancelButtonText = NSLocalizedString("Cancel", comment: "")

            inputBox.onCancel = {
                //Remove blur effects from Superview
                self.blurEffectView.removeFromSuperview()
            }

            inputBox.show()
        }
    }

希望这会对你有所帮助