In my cell i have a button. I added index row and action for this button with following code.
func showAllAction(sender:AnyObject) {
self.performSegueWithIdentifier("showProduct", sender: sender)
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CartCell", forIndexPath:
indexPath) as! CartTableViewCell
cell.showAllButton.tag = indexPath.row
cell.showAllButton.addTarget(self, action: "showAllAction:", forControlEvents: .TouchUpInside)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "showProduct" {
let destinationController = segue.destinationViewController as! ProductController
destinationController.shopItem = self.Items[sender.tag]
}
}
And when user click on button he would go to another page. But when i add new cells to table my indexes are wrong. How recalculate row path each time when i add delete etc..?
Now i delegate this process with NSFetchedResultsControllerDelegate
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch type {
case .Insert:
if let _newIndexPath = newIndexPath {
tableView.insertRowsAtIndexPaths([_newIndexPath], withRowAnimation: .Fade)
}
case .Delete:
if let _indexPath = indexPath {
tableView.deleteRowsAtIndexPaths([_indexPath], withRowAnimation: .Fade)
}
case .Update:
if let _indexPath = indexPath {
tableView.reloadRowsAtIndexPaths([_indexPath], withRowAnimation: .Fade)
}
default:
self.tableView.reloadData()
}
self.items = controller.fetchedObjects as! [Item]
}