1

I add UICollectionView in my app and each cell has "Delete" trash button.
When I click on this button, it will show alert message to ask. After clicks on OK button, the item in Collection View will delete.
However, the UI does not refresh , the item was not deleted.
My Code is ...

override func viewDidLoad() {
    super.viewDidLoad()
    myCollectionView.reloadData()

}

override func viewDidAppear(_ animated: Bool) {
    debugPrint("viewDidAppear")
    callWatchLater()
}

override func viewWillAppear(_ animated: Bool) {
    DispatchQueue.main.async(execute: {
        debugPrint("Appear")
        self.myCollectionView.reloadData()
    })


}

override func viewDidLayoutSubviews() {
    DispatchQueue.main.async(execute: {
        debugPrint("SubViews")
        self.myCollectionView.reloadData()
    })
}

Code for Alert Controller is

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "watchCell", for: indexPath) as! WatchLaterTableCell


        let watchTableList = self.watchLaterTable[indexPath.row]

        cell.deleteMovie.addTarget(self, action:#selector(showAlert(sender:)), for: .touchUpInside)


        }


        return cell
    }

func showAlert(sender:UIButton!)
    {
    debugPrint("Press Button")
    let alert = UIAlertController(title: "Are you really want to delete this movie?", message: "", preferredStyle: UIAlertControllerStyle.alert)



    // Create the actions
    let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
        UIAlertAction in
        self.callRest()
        debugPrint("Press OK")


    }
    let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) {
        UIAlertAction in

        _ = self.navigationController?.popViewController(animated: true)
    }


    // Add the actions
    alert.addAction(okAction)
    alert.addAction(cancelAction)


    // Present the controller

    DispatchQueue.main.async(execute: {
        self.present(alert, animated: true, completion: nil)
    })


}

Code For callRest() is

 func callRest(){
    SwiftLoading().showLoading()
    if Reachability().isInternetAvailable() == true {
        rest.auth(auth: access_token)
        rest.delete(StringResource().mainURL + "user/" + user_id + "/watch/later/" + String(vedioId) , parma: [:], finished: {(result: NSDictionary, status : Int) -> Void in

            debugPrint(result)

            if(status == 200){
                let data = result["data"] as! NSString
                self.messages = String(describing: data)


                DispatchQueue.main.sync{[unowned self] in


                    let alertController = UIAlertController(title: "", message: self.messages , preferredStyle: .alert)

                    // Create the actions
                    let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
                        UIAlertAction in

                        debugPrint("call rest Press OK")
                        self.callWatchLaterGetAPI()
                     }

                    alertController.addAction(okAction)

                    // Present the controller
                    DispatchQueue.main.async(execute: {
                        self.present(alertController, animated: true, completion: nil)
                    })

                }
                self.myCollectionView.reloadData()
                SwiftLoading().hideLoading()
            }else{


                let error = result["error"] as! NSArray

                for item in 0...(error.count) - 1 {

                    let device : AnyObject = error[item] as AnyObject

                    self.messages = device["message"] as! String

                    DispatchQueue.main.sync{[unowned self] in

                    let alertController = UIAlertController(title: "", message: self.messages , preferredStyle: .alert)

                     // Create the actions
                    let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
                            UIAlertAction in

                            debugPrint("Press OK")


                        }

                        alertController.addAction(okAction)

                        // Present the controller
                        DispatchQueue.main.async(execute: {
                            self.present(alertController, animated: true, completion: nil)
                        })
                    }

                    SwiftLoading().hideLoading()
                }
            }
        })

    }else{
        noInternetConnection()
    }
}


However, the CollectionView does not refresh . Can anyone help me this, please?

May Phyu
  • 895
  • 3
  • 23
  • 47
  • Add the action of delete button with alertcontroller – Nirav D May 20 '17 at 09:31
  • What do you mean by **table does not refresh** when you are working with collectionView – Nirav D May 20 '17 at 09:51
  • I click on Delete button, alert box will appear and click Ok button. The background array item list was deducted but the UI was not like that. – May Phyu May 20 '17 at 10:02
  • You don't get me What I'm asking is you are working with collectionView then why you are talking about refreshing tableView – Nirav D May 20 '17 at 10:49
  • @NiravD, Sorry bro. I typed wrong. I edit the question. I mean UICollectionView bro. – May Phyu May 20 '17 at 11:10
  • @Rob is right. provide `callRest` code. – JuicyFruit May 20 '17 at 19:13
  • @Rob, bro I uploaded CallRest function code. – May Phyu May 22 '17 at 02:56
  • Bro @JuicyFruit, I uploaded CallRest function code. – May Phyu May 22 '17 at 02:57
  • While I appreciate your sharing more, this, sadly, _still_ isn't enough to diagnose the problem. But, OMG, I don't want to see more of this code. Let's start over: You should create a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve), with less code, but enough to allow us to reproduce problem. Either make copy of your project and start ripping out everything unrelated to the problem at hand until you're down to the MCVE. Or, better, start with blank project and create simple example that manifests problem. But you can't expect anyone to go through this code. – Rob May 22 '17 at 04:23

0 Answers0