如何在同一个 View Controller 上按下 AlertViewController 上的 OK 按钮后重新加载 Collection View ?
当我点击主屏幕然后再次返回此屏幕时,它会显示已保存的数据吗?当我按下 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()
}
}
最佳答案
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)
关于ios - 在同一个 View Controller 上按下 AlertViewController 上的确定按钮后重新加载 Collection View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48563227/