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