0

I have a serious problem on the first app I am going to publish to the app store. In the app I have an array of object and a table view for them to be shown. in the table view I am displaying all of the object's information on the screen. In each cell I have a button which opens a popover that I made like this(mention this method is connected to the button that each cell has.)`

    @IBAction func performPopOver(sender: UIButton) {

         if let cell = sender.superview!.superview!.superview! as? UITableViewCell {

               var indexPath = tableView.indexPathForCell(cell)!

               currentIndexPath = indexPath.row

               println(indexPath.row)

               var popoverViewController = self.storyboard?.instantiateViewControllerWithIdentifier("Settings") as! UIViewController

               popoverViewController.modalPresentationStyle = .Popover

                popoverViewController.preferredContentSize   = CGSizeMake(130, 75)

               let popoverPresentationViewController = popoverViewController.popoverPresentationController

               popoverPresentationViewController?.permittedArrowDirections = .Right

              popoverPresentationViewController?.delegate = self

              popoverPresentationViewController?.sourceView = cell.contentView

              popoverPresentationViewController?.sourceRect = sender.frame

              presentViewController(popoverViewController, animated: true, completion: nil)

      }

  }

I get the cell in this method and set a global variable called currentIndexPath to the index path of the cell the user just pressed. then creating the popover in the popover I have a delete button which deletes the item with the currentIndexPath from the list and then reload the data and shows the list without the deleted item.

Here is my popover class:

 import UIKit

 @IBDesignable class PopoverView: UIViewController {

      @IBAction func deleteItem(sender: UIButton) {

          self.dismissViewControllerAnimated(true, completion: nil)

          if !learningItemsList.isEmpty {

               learningItemsList.removeAtIndex(currentIndexPath)

          }

          LearningListVC().tableView.reloadData()

      }

learningItemsList is a global variable with all of the objects the user made.

I get an error at the LearningListVC().tableView.reloadData() which says:

fatal error: unexpectedly found nil while unwrapping an Optional value

Here is my table view setup code if it matters:

func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1

}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if learningItemsList.isEmpty {

        self.tableView.contentInset = UIEdgeInsets(top: 10, left: 0, bottom: 0, right: 0)

        return 1

    }else{

        self.tableView.contentInset = UIEdgeInsets(top: 88, left: 0, bottom: 0, right: 0)

        return learningItemsList.count

    }

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("ResultCell", forIndexPath: indexPath) as! ResultCell

    if let learningList = learningItemsList as? [ItemToLearn] {

        if learningList.isEmpty {

            let cell = tableView.dequeueReusableCellWithIdentifier("NoResultCell", forIndexPath: indexPath) as! UITableViewCell

    }else{

            if let toDoItem = learningList[indexPath.row] as? ItemToLearn {

                cell.subjectName.text = toDoItem.title

                cell.descriptionLabel.text = toDoItem.description

                cell.dateLabel.text = toDoItem.dateString

            }

        }

    }

    return cell

}

I guess you already know what my app does from this code but it's alright. P.S. I know the structure is not that good.

One last thing, I think that the problem is when my learningListView appears again from the popover window and the table view property doesn't load before the reloadData() call

Cœur
  • 37,241
  • 25
  • 195
  • 267
arbel03
  • 1,217
  • 2
  • 14
  • 24

1 Answers1

0

This line does not reload the tableview in LearningListVC but creates a new instance of LearningListVC:

LearningListVC().tableView.reloadData()

Dependent on how you are creating the tableview it could be nil at this point.

There are multiple ways to solve this. One is to create a weak var reference to the LearningListVC in the PopoverView controller.

weak var learningListController: LearningListVC?

Then, in LearningListVC, you set self to this learningListController:

var popoverView = PopoverView()
popoverView.learningListController = self

And eventually you can reload the table in PopoverView:

 @IBDesignable class PopoverView: UIViewController {

  @IBAction func deleteItem(sender: UIButton) {
      if !learningItemsList.isEmpty {
           learningItemsList.removeAtIndex(currentIndexPath)
      }

      self.learningListController?.tableView.reloadData()
      self.dismissViewControllerAnimated(true, completion: nil)
  }
Darko
  • 9,655
  • 9
  • 36
  • 48
  • I tried to put reloadData in the view did appear but it doesn't work because the popover isn't a popover segue, I forced swift to make the popover look on an iphone the same as it looks on an iPad so it just shows a view on the screen but no segue happens so the viewWillAppear will never run. – arbel03 Aug 20 '15 at 19:45
  • 1
    Usually this is solved with a delegate, but to keep it simple: put a weak var reference to LearningListVC in the PopOver and then call tableview.reload() on that reference. – Darko Aug 20 '15 at 19:51
  • Like this? weak var learningListController = LearningListVC() learningListController?.tableView.reloadData() – arbel03 Aug 20 '15 at 19:57
  • Now there is no error but the table view still doesn't reload it's data – arbel03 Aug 20 '15 at 20:29
  • Change to self.learningListController!.tableView.reloadData() and see if there is an error. – Darko Aug 20 '15 at 21:05
  • OK, I solved it, using this SO question: http://stackoverflow.com/questions/25921623/how-to-reload-tableview-from-another-view-controller-in-swift – arbel03 Aug 20 '15 at 21:39
  • I don't know how to solve it myself, if you can help me to design it better I will be grateful :) – arbel03 Aug 21 '15 at 10:36