Rahul Chopra
Rahul Chopra

Reputation: 195

Reload collection view after pressing OK button on AlertViewController on the same view controller?

How to reload collection view after pressing OK button on AlertViewController on the same view controller ?

When i tapped Home Screen then again back to this screen , then it shows the saved data? I want to show this data when i pressing OK button on AlertController. I dont know how to solve this issue.

    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()
        }
    }

Upvotes: 0

Views: 473

Answers (2)

Ganesh Manickam
Ganesh Manickam

Reputation: 2139

To reload your collectionView use reloadData() as below

self.collCategory.reloadData()

In your 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()
        }
    }

Hope this will help you

Upvotes: 0

Shehata Gamal
Shehata Gamal

Reputation: 100533

    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)

Upvotes: 2

Related Questions